Android:如何进行单个改造调用,使其显示在不同的标签,标签布局上?

时间:2018-12-18 04:07:07

标签: android retrofit2

我正在创建一个android应用程序,其中将在标签页布局中显示照片。要求每个标签中只能包含20张照片。如何在标签上显示照片?我正在使用viewpager和当前代码。顺便说一下,这是我用来存储数据的网站:https://jsonplaceholder.typicode.com/photos

谢谢!

适配器:

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.MyViewHolder> {

private Context mContext;
List<Photos> photosList = new ArrayList<>();


public PhotoAdapter(Context mContext, List<Photos> photosList){
    this.photosList = photosList;
    this.mContext = mContext;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.photo_card, viewGroup, false);

    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
    Photos photos = photosList.get(i);
    Log.i("Position: ", i+1 + " Id: " + photos.getId());
    myViewHolder.mTitleTextView.setText(photos.getTitle());
    myViewHolder.mIdTextView.setText(photos.getId());
    String thumbnail = photos.getUrl();

    Glide.with(mContext)
            .load(thumbnail)
            .into(myViewHolder.mUrlImageView);

}

@Override
public int getItemCount() {
    //return photosList.size();
    return 20;
}

public class MyViewHolder extends RecyclerView.ViewHolder{

    public TextView mTitleTextView, mIdTextView;
    ImageView mUrlImageView;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        mTitleTextView = (TextView) itemView.findViewById(R.id.titleTextView);
        mIdTextView = (TextView) itemView.findViewById(R.id.idTextView);
        mUrlImageView = (ImageView) itemView.findViewById(R.id.photoImageView);

    }
}

片段:(动态)

public class DyanamicFragment extends Fragment {

private RecyclerView mRecyclerView;
public List<Photos> photos = new ArrayList<>();

public static DyanamicFragment newInstance(){
    return new DyanamicFragment();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dynamic_fragment_layout, container, false);
    initViews(view);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.PhotosRecyclerView);
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),4));


    return view;
}

private void initViews(View view) {
    TextView textView = view.findViewById(R.id.commonTextView);
    textView.setText(String.valueOf("Category: " +getArguments().getInt("position")));

    loadJSON();
}

private void loadJSON(){
    Service serviceAPI = Client.getClient();
    Call<JsonArray> loadPhotosCall = serviceAPI.readPhotosArray();

    loadPhotosCall.enqueue(new Callback<JsonArray>() {
        @Override
        public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
            try {
                String photosString = response.body().toString();
                Type listType = new TypeToken<List<Photos>>() {}.getType();
                photos = getPhotosListFromJson(photosString, listType);

                mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                mRecyclerView.setAdapter(new PhotoAdapter(getContext(), photos));
            }catch (Exception e){
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<JsonArray> call, Throwable t) {

        }
    });
}


public static <T> List<T> getPhotosListFromJson(String jsonString, Type type){
    if (!isValid(jsonString)){
        return null;
    }
    return new Gson().fromJson(jsonString, type);
}

public static boolean isValid(String json){
    try {
        new JsonParser().parse(json);
        return true;
    }catch (JsonSyntaxException jse){
        return false;
    }
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
}

@Override
public void onDestroy() {
    super.onDestroy();
}


}

活动:

public class MainActivity extends AppCompatActivity {

private ViewPager viewPager;
private TabLayout mTabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
}

private void initView() {

    viewPager = findViewById(R.id.viewpager);
    mTabLayout = findViewById(R.id.tabs);
    viewPager.setOffscreenPageLimit(5);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    mTabLayout.setOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    setDynamicFragmentToTabLayout();
}

private void setDynamicFragmentToTabLayout() {
    for (int i = 1; i < 6; i++) {
        if (i == 1){
            mTabLayout.addTab(mTabLayout.newTab().setText("Photos: 1-20"));
        }
        if (i == 2){
            mTabLayout.addTab(mTabLayout.newTab().setText("Photos: 21-40"));
        }
        if (i == 3){
            mTabLayout.addTab(mTabLayout.newTab().setText("Photos: 41-60"));
        }
        if (i == 4){
            mTabLayout.addTab(mTabLayout.newTab().setText("Photos: 61-80"));
        }
        if (i == 5){
            mTabLayout.addTab(mTabLayout.newTab().setText("Photos: 81-100"));
        }
    }
    DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount());
    viewPager.setAdapter(mDynamicFragmentAdapter);
    viewPager.setCurrentItem(0);
}
}

1 个答案:

答案 0 :(得分:0)

您需要在具有视图寻呼机的片段或活动中进行api调用。

然后,您可以解析响应并在将每个片段添加到视图分页器时设置每个片段所需的图像数据。 然后,这些片段可以显示已设置为其列表的数据。