//简单RadioGroup
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radiogroup = findViewById(R.id.radiogroup);
framelayout = findViewById(R.id.framelayout);
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
frag1 = new Frag1();
frag2 = new Frag2();
transaction.add(R.id.framelayout, frag1);
transaction.add(R.id.framelayout, frag2);
transaction.show(frag1).hide(frag2).commit();
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
FragmentTransaction transaction1 = manager.beginTransaction();
switch (i){
case R.id.radio1:
transaction1.show(frag1).hide(frag2).commit();
break;
case R.id.radio2:
transaction1.show(frag2).hide(frag1).commit();
break;
}
}
});
//MainFragment
public class MainFragment extends Fragment {
private String mnr;
private ListView listview;
private ShopAdapte myAdapte;
public static MainFragment getinit(String titles){
MainFragment mainFragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putString("titles",titles);
mainFragment.setArguments(bundle);
return mainFragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mview = inflater.inflate(R.layout.mainfrag, container, false);
Bundle arguments = getArguments();
String titles = arguments.getString("titles");
TextView textView = new TextView(getActivity());
textView.setText(titles);
listview = mview.findViewById(R.id.listview);
switch (titles){
case "头条":
mnr="tt";
new asyn().execute();
break;
case"说唱":
mnr="shehui";
new asyn().execute();
break;
case "傻逼":
mnr="gn";
new asyn().execute();
break;
}
listview.setAdapter(myAdapte);
return mview;
}
class asyn extends AsyncTask<Integer,Integer,String>{
String mPath="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri="+mnr+"";
@Override
protected String doInBackground(Integer... integers) {
return Httpcon.gethttp(getActivity(),mPath,"GET");
}
@Override
protected void onPostExecute(String s) {
if (s != null) {
Gson gson = new Gson();
ShopBean focusBean = gson.fromJson(s,ShopBean.class);
ShopBean.ResultBean resultBean ;
resultBean = focusBean.getResult();
if (resultBean != null) {
myAdapte = new ShopAdapte(getActivity(), resultBean);
listview.setAdapter(myAdapte);
}
}
}
}
//布局fragment1中的
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mview = inflater.inflate(R.layout.frag1, container, false);
viewpager = mview.findViewById(R.id.viewpager);
hs = mview.findViewById(R.id.hs);
linear = mview.findViewById(R.id.linearlayout);
titles =new String []{"头条","说唱","傻逼"};
viewpager.setAdapter(new ViewPagerad(getActivity().getSupportFragmentManager(),titles));
list = new ArrayList<>();
for (int i=0;i<titles.length;i++){
TextView textView = new TextView(getActivity());
textView.setText(titles[i]);
textView.setTextSize(20);
textView.setTextColor(Color.rgb(250,250,250));
textView.setId(i);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
viewpager.setCurrentItem(id);
}
});
if(i ==0){
textView.setTextColor(Color.RED);
}else {
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(120,140,20,40);
linear.addView(textView,layoutParams);
list.add(textView);
}
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//滑动
@Override
public void onPageScrolled(int position, float v, int i1) {
//list中存的是 textview
for (int i = 0; i < list.size(); i++) {
if(position==i){
list.get(i).setTextColor(getResources().getColor(R.color.colorAccent));
}else{
list.get(i).setTextColor(getResources().getColor(R.color.colorPrimary));
}
}
//获取当前的textview
TextView textView = list.get(position);
// //width是每次滑动的距离
int width = textView.getWidth()+10;
// //让scrollView滑动 滑动距离是textview之间的间距
hs.scrollTo(width*position,0);
}
//选中
@Override
public void onPageSelected(int i) {
}
//改变
@Override
public void onPageScrollStateChanged(int i) {
}
});
return mview;
}
//viewPagerAdapter
public class ViewPagerad extends FragmentPagerAdapter { String [] titles; public ViewPagerad(FragmentManager fm, String titles[]) { super(fm); this.titles=titles; } @Override public Fragment getItem(int position) { return MainFragment.getinit(titles[position]); } @Override public int getCount() { return titles.length; } }