效果图展示
1.activity_main.xml
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:textColor="#FFFFFF" android:textSize="23sp" android:visibility="gone" />
2.MainActivity中
public class MainActivity extends AppCompatActivity implements MyWelfareAdapter.OnClickListener {
private RecyclerView lv;
private ArrayList
private MyWelfareAdapter adapter;
private String url = "接口地址";
private static final String TAG = "MainActivity----";
private ArrayList
private ViewPager vp;
private TextView txt;
private MyPagerAdapter pagerAdapter;
private ImageView img;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
OkHttpClient httpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url(url)
.build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
final Gson gson = new Gson();
WelfareBean welfareBean = gson.fromJson(string, WelfareBean.class);
list.addAll(welfareBean.getResults());
adapter.notifyDataSetChanged();
}
});
}
});
}
private void initView() {
lv = (RecyclerView) findViewById(R.id.lv);
vp = (ViewPager) findViewById(R.id.vp);
txt = (TextView) findViewById(R.id.txt);
list = new ArrayList<>();
viewList = new ArrayList<>();
adapter = new MyWelfareAdapter(this, list);
lv.setAdapter(adapter);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);//不设置的话,图片闪烁错位,有可能有整列错位的情况。
lv.setLayoutManager(layoutManager);
pagerAdapter = new MyPagerAdapter(this, viewList);
vp.setAdapter(pagerAdapter);
//RecyclerView的监听方法
adapter.setOnClickListener(this);
//ViewPager的监听方法
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
txt.setText(i + 1 + "/" + 20);//索引是从0开始,所以让i+1
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
@Override
public void OnClick(int position, WelfareBean.ResultsBean resultsBean, ArrayList
//点击Item条目时让RecyclerView隐藏,ViewPager和TextView显示
lv.setVisibility(View.GONE);
vp.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
this.position = position;
for (int i = 0; i < list.size(); i++) {
//自定义一个布局,用于显示图片的.
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_image, null);
img = view.findViewById(R.id.img);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//当点击图片的时候让RecyclerView显示,vp和txt隐藏
lv.setVisibility(View.VISIBLE);
vp.setVisibility(View.GONE);
txt.setVisibility(View.GONE);
}
});
//点击哪个图片显示哪个图片
vp.setCurrentItem(position);
Glide.with(MainActivity.this).load(list.get(i).getUrl()).into(img);
viewList.add(view);
}
pagerAdapter.notifyDataSetChanged();
}