viewpager同步


package bawei.com.xliscom.administrator.maney;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private TextView t1,t2,t3,tt1,tt,tt2,tt3;
private List<TextView> list1=new ArrayList<>();
private List<TextView> list2=new ArrayList<>();
private ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1= (TextView) findViewById(R.id.ta);
t2= (TextView) findViewById(R.id.tb);
t3= (TextView) findViewById(R.id.tc);
tt1= (TextView) findViewById(R.id.taa);
tt2= (TextView) findViewById(R.id.tbb);
tt3= (TextView) findViewById(R.id.tcc);
list1.add(t1);
list1.add(t2);
list1.add(t3);
list2.add(tt1);
list2.add(tt2);
list2.add(tt3);
t1.setOnClickListener(this);
t2.setOnClickListener(this);
t3.setOnClickListener(this);
tt1.setVisibility(View.VISIBLE);
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
Fragment frag=null;
switch (position){
case 0:
frag=new Fragment01();
changge(list2.get(position).getId());

break;
case 1:
frag=new Fragment01();
changge(list2.get(position).getId());
break;
case 2:
frag=new Fragment01();
changge(list2.get(position).getId());
break;
}
return frag;
}
@Override
public int getCount() {
return 3;
}
});
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
changge(list2.get(position).getId());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void changge(int id){
for (int i = 0; i <list2.size() ; i++) {
if(list2.get(i).getId()==id){
list2.get(i).setVisibility(View.VISIBLE);
}else{
list2.get(i).setVisibility(View.GONE);
}
}
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ta:
changge(list2.get(0).getId());
viewpager.setCurrentItem(0);
break;
case R.id.tb:
changge(list2.get(1).getId());
viewpager.setCurrentItem(1);
break;
case R.id.tc:
changge(list2.get(2).getId());
viewpager.setCurrentItem(2);
break;

}

}
}

0 個評論

要回覆文章請先登錄註冊