Android Studio 1-6 Fragment 阶段完成
ViewPager 与 Fragment 结合应用
java代码
package com.example.xiaoxiaodelianxi;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
private RadioButton btn_PageOne;
private RadioButton btn_PageTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_PageOne = (RadioButton) findViewById(R.id.btn_PageOne);
btn_PageTwo = (RadioButton) findViewById(R.id.btn_PageTwo);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.layout_frame,new oneFragment());
fragmentTransaction.commit();
//来到第一个fragment
btn_PageOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_frame,new oneFragment());
fragmentTransaction.commit();
}
});
//来到第二个fragment
btn_PageTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_frame,new twoFragment());
fragmentTransaction.commit();
}
});
}
}
package com.example.xiaoxiaodelianxi;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.util.List;
public class MyAdapter extends PagerAdapter {
private List<String> list;
private Context context;
public MyAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View inflate = LayoutInflater.from(context).inflate(R.layout.head_xiao_layout, container, false);
ImageView imageView = inflate.findViewById(R.id.xiao_image_iv);
Glide.with(context).load(list.get(position)).into(imageView);
container.addView(inflate);
return inflate;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
}
package com.example.xiaoxiaodelianxi;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* A simple {@link Fragment} subclass.
*/
public class oneFragment extends Fragment {
private List<String> list = new ArrayList<>();
private Handler handler = new Handler();
public oneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
final ViewPager viewById1 = inflate.findViewById(R.id.tv_vp);
LinearLayout lll = inflate.findViewById(R.id.tv_lin);
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565169763263&di=ae8dbb5f2e6270d32ef7eb4e4c83b397&imgtype=0&src=http%3A%2F%2Fwww.chinadaily.com.cn%2Fhqzx%2Fimages%2Fattachement%2Fjpg%2Fsite385%2F20120924%2F00221918200911ca40e52b.jpg");
// list.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3925233323,1705701801&fm=26&gp=0.jpg");
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565169830035&di=c454f4960947096f631e9cb5bf63a489&imgtype=0&src=http%3A%2F%2Fd-pic-image.yesky.com%2F1080x-%2FuploadImages%2F2019%2F044%2F59%2F1113V6L3Q6TY.jpg");
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565169830034&di=0aa6ffe6f42c2ca7f664f77184bf1ac4&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201806%2F17%2F20180617193912_aqlos.png");
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565170260731&di=d565352ef002d104e9fc9735c7d4cca7&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F71ff69934501746b5655368f0e4fc84495e41f96de27-CIJh6T_fw658");
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565170260730&di=9a1a3fd2c202bb6d69f4572ecb72e62d&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20180712%2Fd628872635b345ae9521093eb03dc441.jpg");
// list.add("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=4211603615,2895194094&fm=26&gp=0.jpg");
// list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1565160309&di=2312609da814b2b54e63d5af08b07429&src=http://s1.sinaimg.cn/mw690/006yJCDszy74tJGvkHK30&690");
list.add("http://b-ssl.duitang.com/uploads/item/201804/24/20180424220005_MJLwz.png");
list.add("http://b-ssl.duitang.com/uploads/item/201804/24/20180424214700_UtMaV.png");
list.add("http://b-ssl.duitang.com/uploads/item/201612/13/20161213154536_AGv84.jpeg");
list.add("http://b-ssl.duitang.com/uploads/item/201804/24/20180424214700_ZTXVy.png");
MyAdapter myAdapter = new MyAdapter(list, getActivity());
viewById1.setAdapter(myAdapter);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int count = -1;
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
viewById1.setCurrentItem(++count);
}
});
if (count == list.size()){
count = -1;
}
}
},0,2000);
final ArrayList<ImageView> integers = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(R.drawable.drw_false);
lll.addView(imageView);
integers.add(imageView);
}
integers.get(0).setImageResource(R.drawable.drw_true);
viewById1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (int j = 0; j < integers.size(); j++) {
if (j == i){
integers.get(j).setImageResource(R.drawable.drw_true);
}else {
integers.get(j).setImageResource(R.drawable.drw_false);
}
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
return inflate;
}
}
package com.example.xiaoxiaodelianxi;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class twoFragment extends Fragment {
public twoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_two, container, false);
Button button = inflate.findViewById(R.id.btn_text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.tanchuang_layout, null);
Button btuok = inflate.findViewById(R.id.ok);
Button btuno = inflate.findViewById(R.id.no);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
btuno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "确认", Toast.LENGTH_SHORT).show();
builder.create().dismiss();
}
});
btuok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
builder.create().dismiss();
}
});
builder.setView(inflate);
builder.create().show();
}
});
return inflate;
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<FrameLayout
android:id="@+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
</FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btn_PageOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="首页"
android:textSize="30sp" />
<RadioButton
android:id="@+id/btn_PageTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="我的"
android:textSize="30sp" />
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".oneFragment">
<android.support.v4.view.ViewPager
android:id="@+id/tv_vp"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
android:adjustViewBounds="true"
/>
<LinearLayout
android:id="@+id/tv_lin"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignBottom="@id/tv_vp"></LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
tools:context=".twoFragment">
<Button
android:id="@+id/btn_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="评一评"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/xiao_image_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"/>
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
</LinearLayout>