我是Android开发的新手,在将ImageView添加到RecyclerView列表中时遇到了麻烦。我已经为此工作了几个月,但仍在努力寻找解决方案。
有没有人可以帮助我解决这个问题?下面是我的MainActivity.java文件供参考:
package com.example.customlistview;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivicty";
private Context mcontext;
ArrayList<String> titleArrayList;
RecyclerView mRecyclerview;
int images[] = {R.drawable.facebook, R.drawable.whatsapp, R.drawable.twitter, R.drawable.instagram, R.drawable.youtube};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleArrayList=new ArrayList<String>();
titleArrayList.add(Constants.NUMBER_SYSTEM);
titleArrayList.add(Constants.POWER_ICDICES);
titleArrayList.add(Constants.SIMPLIFICATION);
titleArrayList.add(Constants.ALGEBRA);
;
mcontext=MainActivity.this;
mRecyclerview=(RecyclerView) findViewById(R.id.Recycle);
mRecyclerview.setHasFixedSize(true);
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
TitleAdapter adapter=new TitleAdapter(mcontext, titleArrayList, new CustomItemClickListener() {
@Override
public void onItemClick(View v, int i) {
Toast.makeText(mcontext, "clicked"+titleArrayList.get(i),Toast.LENGTH_SHORT).show();
}
});
mRecyclerview.setAdapter(adapter);
}
}
TitleApdapter.java
package com.example.customlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.MyViewHolder> {
private Context mcontext;
private ArrayList<String> titleList;
private CustomItemClickListener clickListener;
public TitleAdapter(Context mcontext, ArrayList<String> titleList, CustomItemClickListener clickListener) {
this.mcontext = mcontext;
this.titleList = titleList;
this.clickListener = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view= LayoutInflater.from(mcontext).inflate(R.layout.row,parent,false);
final MyViewHolder viewHolder=new MyViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(view,viewHolder.getPosition());
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.titletext.setText(titleList.get(position).replace("_"," "));
}
@Override
public int getItemCount() {
return titleList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titletext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titletext=(TextView) itemView.findViewById(R.id.textView1);
}
}
}
Constant.java
package com.example.customlistview;
class Constants {
static final String NUMBER_SYSTEM="NUMBER_SYSTEM(संख्या पद्धति)";
static final String POWER_ICDICES="POWER,ICDICES & SURDS";
static final String SIMPLIFICATION="SIMPLIFICATION(संख्या पद्धति)";
static final String ALGEBRA="ALGEBRA(बीजगणित)";
}
CustomItemClickListener.java
package com.example.customlistview;
import android.view.View;
public interface CustomItemClickListener {
public void onItemClick(View v,int i);
}
这是我的row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:contentPadding="5dp"
app:cardElevation="3dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/whiteColor"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/whatsapp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="@string/main_title"
android:textColor="@color/blackColor"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
任何指导都将不胜感激。提前感谢您。
1条答案
按热度按时间9lowa7mx1#
我遇到过一个类似的问题,并成功地解决了它。现在我可以给予你我的代码。我想它会帮助你
ListAdapter.java
ItemAdapter.java
MainActivity.java
activity_main.xml
item_custom.java