[英]How to set empty adapter and re-bind adapter to RecyclerView when data load from server
如何在訪問fragment
時注冊一個empty adapters
,並在服務器接收到數據后如何將其重新綁定到RecyclerView
?
我仍然對將適配器設置為空感到困惑,然后當我連接到數據庫時,當我使用RecyclerView時結果將可見
也許有人可以教我如何實現這一目標
我的適配器
package adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.transvision.bertho.transvisiondashboardapp.R;
import java.util.List;
import model.Channel;
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {
private List<Channel> channels;
private int rowLayout;
private Context context;
public static class ChannelViewHolder extends RecyclerView.ViewHolder {
LinearLayout moviesLayout;
TextView movieTitle;
TextView data;
TextView movieDescription;
TextView rating;
TextView name;
TextView code;
TextView description;
TextView number;
TextView definition;
TextView paket;
ImageView logo;
public ChannelViewHolder(View v) {
super(v);
moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
name = (TextView) v.findViewById(R.id.title);
definition = (TextView) v.findViewById(R.id.subtitle);
description = (TextView) v.findViewById(R.id.description);
}
}
public ChannelAdapter(List<Channel> channels, int rowLayout, Context context) {
this.channels = channels;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public ChannelAdapter.ChannelViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new ChannelViewHolder(view);
}
@Override
public void onBindViewHolder(ChannelViewHolder holder, final int position) {
holder.name.setText(channels.get(position).getName());
holder.definition.setText(channels.get(position).getDefinition());
holder.description.setText(channels.get(position).getDescription());
}
@Override
public int getItemCount() {
return channels.size();
}
}
我的片段
package page;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.transvision.bertho.transvisiondashboardapp.R;
import java.util.ArrayList;
import java.util.List;
import adapter.ChannelAdapter;
import model.Channel;
import model.ChannelResponse;
import rest.ApiClient;
import rest.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private static final String TAG = HomeFragment.class.getSimpleName();
private ChannelAdapter adapter;
List<Channel> listChannel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
adapter = new ChannelAdapter (listChannel, R.layout.list_channel, getActivity());
recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
getChannelData();
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
public void getChannelData() {
ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
Call<ChannelResponse> call = apiService.getItems();
call.enqueue(new Callback<ChannelResponse>() {
@Override
public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
int statusCode = response.code();
List<Channel> channel = response.body().getItems();
recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getActivity()));
adapter.notifyDataSetChanged();
showToast("CONNECTION SUCCESS");
}
@Override
public void onFailure(Call<ChannelResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
showToast("CONNECTION ERROR");
}
});
}
public void showToast(String output){
Toast.makeText(getActivity(), output, Toast.LENGTH_SHORT).show();
}
}
通過使用notifyDataSetChanged()
向適配器通知數據已更改的正確方法又如何呢?
請幫助我了解過程
謝謝
使用空的頻道列表初始化ChannelAdapter並將其設置為recyclerview。 然后,當您從api接收頻道列表時,請更新適配器中的列表,然后調用notifyDataSetChanged()。 這將更新recyclerview。 您不必創建新的適配器並重新設置。
希望您能得到想要的東西:在適配器中,
public void addItems(List<SomeTeam> list){
this.list.addAll(list);
notifyDataSetChanged();
}
public void clearItems(){
teams.clear();
}
您可以在將適配器中的列表設置為時在活動中調用此方法:
public void setSomeTeamList(List<SomeTeam>teamList){
teamAdapter.clearItems();
teamAdapter.addItems(teamList);
}
希望您能從上面的示例代碼中得到啟發。
首先初始化您的列表
List<Channel> listChannel = new ArrayList<Channel>();
現在在您的getChannelData()
您正在創建新適配器,同時將其設置為recyclerView
,盡管您已經在onCreateView()
創建了它
recyclerView.setAdapter(adapter); //pass your adapter object instead of creating new object.
adapter.notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.