KBJ KBJ - 2 months ago 11
Android Question

The distance between card view is too large

I am using cardview with recycler view.But earlier I used the same code to do things without any problems.But now I am using fragments. Now I am getting the distance between the cardviews are greater.
my recycler view adapter

public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView label;
TextView dateTime;
ImageView imageView;
ProgressBar progressBar;

public DataObjectHolder(final View itemView, final Context activity, final ArrayList<DataObject> myDataset) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
//dateTime = (TextView) itemView.findViewById(R.id.thot);
imageView=(ImageView)itemView.findViewById(R.id.iproductimg);
progressBar=(ProgressBar)itemView.findViewById(R.id.progress);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);

}

@Override
public void onClick(View v) {
myClickListenerhi.onItemClick(getLayoutPosition(),v);
}
}



public MyRecyclerViewAdapter(Activity context, ArrayList<DataObject> myDataset,MyClickListener myClickListener1) {
mDataset = myDataset;
activityContext=context;
myClickListenerhi=myClickListener1;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view,activityContext,mDataset);
return dataObjectHolder;
}

@Override
public void onBindViewHolder(final DataObjectHolder holder, int position) {
Context context=null;
holder.label.setText(mDataset.get(position).getmText1());
// holder.dateTime.setText(mDataset.get(position).getmText2());
Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
holder.imageView.setImageDrawable(placeholder);
//new ImageDownloaderTask(holder.imageView).execute(mDataset.get(position).getUrl());
mDataset.get(position).setPosition(position);
Picasso.with(activityContext)
.load(mDataset.get(position).getUrl())
.placeholder(R.drawable.placeholder)
.into(holder.imageView, new Callback() {
@Override
public void onSuccess() {
holder.progressBar.setVisibility(View.INVISIBLE);
}

@Override
public void onError() {
}
});
}

public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

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

public interface MyClickListener {
void onItemClick(int position, View v);
}

}


my cardview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
>

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="156dp"
android:layout_height="242dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="5dp"
card_view:cardBackgroundColor="#ffffff">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="@+id/iproductimg"
android:src="@drawable/placeholder"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:scaleType="fitXY" />
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"
android:indeterminate="false" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:id="@+id/firstheadtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>
</android.support.v7.widget.CardView>


</LinearLayout>


The error
enter image description here

KBJ KBJ
Answer

I solved this by changing cardview linearlayout attribute from

android:layout_width="match_parent"
 android:layout_height="match_parent"

to

 android:layout_width="wrap_content"
 android:layout_height="wrap_content"