gpt4 book ai didi

android - 尝试学习如何将 getItemViewType() 与 RecyclerView 一起使用

转载 作者:行者123 更新时间:2023-11-30 00:18:47 29 4
gpt4 key购买 nike

因此,在有人责备我问一个已经问过的问题之前,我已经在这个站点上查找了一个关于如何使用方法 getItemViewType() 在 RecyclerView 中呈现不同列表项的解决方案。我正在尝试了解它是如何工作的,因为我什至看到了 RecyclerView 实现本身的变体,但这是我的问题。在我的 onBindViewHolder 方法中,它无法识别我创建的每个预期 ViewHolder 的 bind() 方法。谁能告诉我为什么?

public class CrimeListFragment extends Fragment {

private static final int ITEM_TYPE_NORMAL = 0;
private static final int ITEM_TYPE_COPS = 1;

private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;



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

mCrimeRecyclerView = (RecyclerView) view
.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

updateUI();

return view;
}

private void updateUI() {
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();

mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}



private class CrimeHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {

private Crime mCrime;

private TextView mTitleTextView;
private TextView mDateTextView;

public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime, parent, false));
itemView.setOnClickListener(this);

mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
}

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
}

@Override
public void onClick(View view) {
Toast.makeText(getActivity(),
mCrime.getTitle() + " clicked!", Toast.LENGTH_SHORT)
.show();
}
}

private class PoliceHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{

private Crime mCrime;

private TextView mTitleTextView;
private TextView mDateTextView;
private Button mPoliceButton;

public PoliceHolder(LayoutInflater inflater, ViewGroup parent){
super(inflater.inflate(R.layout.list_item_crime_police, parent, false));
itemView.setOnClickListener(this);

mTitleTextView = (TextView)itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView)itemView.findViewById(R.id.crime_date);
mPoliceButton = (Button)itemView.findViewById(R.id.police_button);
mPoliceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "call the police!", Toast.LENGTH_SHORT).show();
}
});
}

public void bind(Crime crime){
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
}

@Override
public void onClick(View view){
Toast.makeText(getActivity(), mCrime.getTitle() + "Clicked!", Toast.LENGTH_SHORT).show();
}


}


private class CrimeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Crime> mCrimes;

public CrimeAdapter(List<Crime> crimes) {
mCrimes = crimes;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

if(viewType == ITEM_TYPE_COPS){
return new PoliceHolder(layoutInflater, parent);
}
if (viewType == ITEM_TYPE_NORMAL) {
return new CrimeHolder(layoutInflater, parent);
}

return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Crime crime = mCrimes.get(position);
final int itemType = getItemViewType(position);

if(itemType == ITEM_TYPE_COPS){
(PoliceHolder)holder.bind(crime);
}
if(itemType == ITEM_TYPE_NORMAL){
(CrimeHolder)holder.bind(crime);
}

}

@Override
public int getItemCount() {
return mCrimes.size();
}

@Override
public int getItemViewType(int position){
if (mCrimes.get(position).isRequiresPolice()){
return ITEM_TYPE_COPS;
} else {
return ITEM_TYPE_NORMAL;
}
}

}
}

最佳答案

问题是您没有正确地将 ViewHolder 转换为您创建的类。您的方法应如下所示:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Crime crime = mCrimes.get(position);
final int itemType = getItemViewType(position);

if(itemType == ITEM_TYPE_COPS){
((PoliceHolder)holder).bind(crime);
}
if(itemType == ITEM_TYPE_NORMAL){
((CrimeHolder)holder).bind(crime);
}
}

关于android - 尝试学习如何将 getItemViewType() 与 RecyclerView 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46698897/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com