仅在第一个recyclerview项目中显示特定文本[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我有一个recyclerview,它将在设备中安装apk并使用cardview对它们进行排序,因为标题说我想在第一个cardview项目上显示特定文本(recyclerview viewholder)

这是我的代码:

private Context context1;
private List<String> stringList;

IZIGaapsAppRecycleView(Context context, List<String> list) {

    context1 = context;
    stringList = list;
}

static class ViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;
    ImageView imageView;
    TextView textView_App_Name;

    ViewHolder(View view) {
        super(view);

        cardView = view.findViewById(R.id.card_view);
        imageView = view.findViewById(R.id.imageView);
        textView_App_Name = view.findViewById(R.id.Apk_Name);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view2 = LayoutInflater.from(context1).inflate(R.layout.cardview_layout, parent, false);
    return new ViewHolder(view2);
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    IZIGaapsApkInfo IZIGaapsApkInfo = new IZIGaapsApkInfo(context1);
    final String ApplicationPackageName = stringList.get(position);
    String ApplicationLabelName = IZIGaapsApkInfo.GetAppName(ApplicationPackageName);
    Drawable drawable = IZIGaapsApkInfo.getAppIconByPackageName(ApplicationPackageName);
    viewHolder.textView_App_Name.setText(ApplicationLabelName);
    viewHolder.imageView.setImageDrawable(drawable);
    viewHolder.cardView.setOnClickListener(view ->

            Snackbar.make(view, ApplicationLabelName, Snackbar.LENGTH_LONG).setAction("ط´ط±ظˆط¹ ط¶ط¨ط·", view1 -> {
                Intent openingApp = new Intent(context1, IZIGaapsMPPermissions.class);
                openingApp.putExtra(IZIGaapsConst.APP_ID, ApplicationPackageName);
                context1.startActivity(openingApp);
            }).show());
}

@Override
public int getItemCount() {

    return stringList.size();
}

}

任何帮助将非常感谢

android android-recyclerview android-cardview
1个回答
0
投票

我找到了自己的解决方案

只需添加一个switch语句来获取位置并在该特定位置上做一些额外的事情,如下所示:

private Context context1;
private List<String> stringList;

IZIGaapsAppRecycleView(Context context, List<String> list) {

    context1 = context;
    stringList = list;
}

static class ViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;
    ImageView imageView;
    TextView textView_App_Name;
    TextView textView_specific_text;

    ViewHolder(View view) {
        super(view);

        cardView = view.findViewById(R.id.card_view);
        imageView = view.findViewById(R.id.imageView);
        textView_App_Name = view.findViewById(R.id.Apk_Name);
        textView_specific_text =  view.findViewById(R.id.texttwo);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view2 = LayoutInflater.from(context1).inflate(R.layout.cardview_layout, parent, false);
    return new ViewHolder(view2);
}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    IZIGaapsApkInfo IZIGaapsApkInfo = new IZIGaapsApkInfo(context1);
    final String ApplicationPackageName = stringList.get(position);
    String ApplicationLabelName = IZIGaapsApkInfo.GetAppName(ApplicationPackageName);
    Drawable drawable = IZIGaapsApkInfo.getAppIconByPackageName(ApplicationPackageName);
    viewHolder.textView_App_Name.setText(ApplicationLabelName);

    //added a switch statement to getting position and doing some extra stuff on that specific position
    switch(position){
      case 0:
      viewHolder.textView_specific_text.setText(mytext);
      break;
    }

    viewHolder.imageView.setImageDrawable(drawable);
    viewHolder.cardView.setOnClickListener(view ->

            Snackbar.make(view, ApplicationLabelName, Snackbar.LENGTH_LONG).setAction("ط´ط±ظˆط¹ ط¶ط¨ط·", view1 -> {
                Intent openingApp = new Intent(context1, IZIGaapsMPPermissions.class);
                openingApp.putExtra(IZIGaapsConst.APP_ID, ApplicationPackageName);
                context1.startActivity(openingApp);
            }).show());
}

@Override
public int getItemCount() {

    return stringList.size();
}

}
© www.soinside.com 2019 - 2024. All rights reserved.