0

我有两个活动,每个活动都绑定到同一个适配器,当我单击项目时,它会打开带有所有数据的第二个活动,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>

4

2 回答 2

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);

    }
于 2016-12-23T04:48:51.400 回答
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 中。

于 2016-12-23T05:04:03.313 回答