我有两个活动,每个活动都绑定到同一个适配器,其中一个当我点击项目时打开第二个活动包含所有数据,cardview宽度是wrap_content它在第一个活动中正常工作但在第二个我想要宽度要match_parent
我将布局设为水平我尝试setLayoutParams()
但是没有用?
public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cardView=(CardView)parent.findViewById(R.id.cardview);
CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
cardView.setLayoutParams(layoutParams);
...
我有这个错误
java.lang.NullPointerException
at com.nourabosily.aa.azkaar.AzkaarAdapter.onCreateViewHolder(AzkaarAdapter.java:37)
at com.nourabosily.aa.azkaar.AzkaarAdapter.onCreateViewHolder(AzkaarAdapter.java:23)
这是适配器的代码
public class AzkaarAdapter extends RecyclerView.Adapter<AzkaarAdapter.AzkaarViewHolder> {
ArrayList<AzkarForm> azkarForms=new ArrayList<AzkarForm>();
Context ctx;
public AzkaarAdapter (ArrayList<AzkarForm> azkarForms,Context ctx){
this.azkarForms=azkarForms;
this.ctx=ctx;
}
@Override
public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { CardView cardView=(CardView)parent.findViewById(R.id.cardview);
CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
cardView.setLayoutParams(layoutParams);
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
AzkaarViewHolder azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);
ViewGroup viewGroup=(ViewGroup)parent.getParent();
CardView.LayoutParams params=(CardView.LayoutParams)(viewGroup.getLayoutParams());
params.height=800;
return azkaarViewHolder;
}
@Override
public void onBindViewHolder(AzkaarViewHolder holder, int position) {
AzkarForm azkarForm=azkarForms.get(position);
if(ctx instanceof MainActivity) {
holder.sample.setText(azkarForm.getSample());
holder.number.setText(String.valueOf(azkarForm.getNumber()));
}else{
holder.sample.setText(azkarForm.getComplete_morning());
holder.number.setText(String.valueOf(azkarForm.getNumber()));
}
int width =holder.itemView.getWidth();
ViewGroup.LayoutParams params=holder.itemView.getLayoutParams();
params.height= RelativeLayout.LayoutParams.MATCH_PARENT;
holder.itemView.setLayoutParams(params);
}
@Override
public int getItemCount() {
return azkarForms.size();
}
public static class AzkaarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView sample, number;
Context ctx;
ArrayList<AzkarForm> azkarForms = new ArrayList<AzkarForm>();
public AzkaarViewHolder(View itemView, Context ctx, ArrayList<AzkarForm> azkarForms) {
super(itemView);
this.ctx = ctx;
if(this.ctx instanceof MainActivity)
itemView.setOnClickListener(this);
this.azkarForms = azkarForms;
sample = (TextView) itemView.findViewById(R.id.sample);
number = (TextView) itemView.findViewById(R.id.number);
}
@Override
public void onClick(View v) {
int position=getAdapterPosition();
AzkarForm azkarForm=azkarForms.get(position);
Intent intent=new Intent(this.ctx,DetailsActivity.class);
intent.putExtra("numbers",azkarForm.getNumber());
intent.putExtra("sample",azkarForm.getSample());
intent.putExtra("complete",azkarForm.getComplete_morning());
intent.putExtra("position",position);
this.ctx.startActivity(intent);
}
}
}
这是布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearlayout">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/relative">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sample"
android:text="Noura Ali Bosily"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
android:layout_alignParentRight="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/number"
android:text="5"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:0)
@Override
public AzkaarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
AzkaarViewHolder azkaarViewHolder=new AzkaarViewHolder(view,ctx,azkarForms);
return azkaarViewHolder;
}
@Override
public void onBindViewHolder(AzkaarViewHolder holder, int position) {
CardView.LayoutParams layoutParams = (CardView.LayoutParams)holder.cardView.getLayoutParams();
layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
holder.cardView.setLayoutParams(layoutParams);
AzkarForm azkarForm=azkarForms.get(position);
ViewGroup viewGroup=(ViewGroup)holder.cardview.getParent();
CardView.LayoutParams params=(CardView.LayoutParams)(viewGroup.getLayoutParams());
params.height=800;
if(ctx instanceof MainActivity) {
holder.sample.setText(azkarForm.getSample());
holder.number.setText(String.valueOf(azkarForm.getNumber()));
}else{
holder.sample.setText(azkarForm.getComplete_morning());
holder.number.setText(String.valueOf(azkarForm.getNumber()));
}
int width =holder.itemView.getWidth();
ViewGroup.LayoutParams params=holder.itemView.getLayoutParams();
params.height= RelativeLayout.LayoutParams.MATCH_PARENT;
holder.itemView.setLayoutParams(params);
}
public static class AzkaarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView sample, number;
Context ctx;
CardView cardView
ArrayList<AzkarForm> azkarForms = new ArrayList<AzkarForm>();
public AzkaarViewHolder(View itemView, Context ctx, ArrayList<AzkarForm> azkarForms) {
super(itemView);
this.ctx = ctx;
if(this.ctx instanceof MainActivity)
itemView.setOnClickListener(this);
this.azkarForms = azkarForms;
sample = (TextView) itemView.findViewById(R.id.sample);
number = (TextView) itemView.findViewById(R.id.number);
cardView=(CardView)parent.findViewById(R.id.cardview);
}
@Override
public void onClick(View v) {
int position=getAdapterPosition();
AzkarForm azkarForm=azkarForms.get(position);
Intent intent=new Intent(this.ctx,DetailsActivity.class);
intent.putExtra("numbers",azkarForm.getNumber());
intent.putExtra("sample",azkarForm.getSample());
intent.putExtra("complete",azkarForm.getComplete_morning());
intent.putExtra("position",position);
this.ctx.startActivity(intent);
}
答案 1 :(得分:0)
CardView.LayoutParams layoutParams = (CardView.LayoutParams)cardView.getLayoutParams();
layoutParams.height = CardView.LayoutParams.MATCH_PARENT;
cardView.setLayoutParams(layoutParams);
而是尝试使用,
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
holder.cardView.setLayoutParams(params);
在
中完成@Override
public void onBindViewHolder(AzkaarViewHolder holder, int position) {
cardView应该在ViewHolder中。