毕加索:为什么我的cardview会在图像加载时改变其尺寸?

时间:2017-05-29 08:37:21

标签: java android picasso

我使用Picasso加载我的Image而我的问题是关于我的cardview它在Image加载时改变了它的Dimension。看起来像这样;
image(1) 之前加载并Image(2)之后看起来像这样 即使图像加载,如何修复尺寸。这是我的代码

drawer_layout_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="128dp"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="3dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="3">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="2">

            <ImageView
                android:id="@+id/partner_post_image"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_margin="8dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:weightSum="3"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/partner_store"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                android:gravity="bottom|start"
                android:text="Store Name"
                android:textAlignment="viewStart"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/partner_post_desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_weight="2"
                android:gravity="top|start"
                android:text="Description Here"
                android:textAlignment="viewStart"
                android:textColor="#000"
                android:textSize="12sp" />

        </LinearLayout>
    </LinearLayout>

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

我的RecyclerView.Holder代码就像这样

public class PartnerStoreViewHolder extends RecyclerView.ViewHolder {

    private View mView;
    public PartnerStoreViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setStore(String store ){
        TextView storeName = (TextView) mView.findViewById(R.id.partner_store);
        storeName.setText(store);
    }
    public void setDescription(String description){
        TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
        descName.setText(description);
    }
    public void setImage(Context context, String image){
        ImageView postImage = (ImageView) mView.findViewById(R.id.partner_post_image);
        Picasso.with(context)
                .load(image)
                .error(android.R.drawable.stat_notify_error)
                .placeholder(R.drawable.ic_blu_logo)
                .fit()
                .into(postImage);
    }
}

我的片段代码和适配器

public class PartnerStore extends BaseFragment {
    private RecyclerView recyclerView;
    private FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder> firebaseRecyclerAdapter;
    private DatabaseReference mRef;

    public PartnerStore() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.drawer_partner_store, container, false);

        mRef = MyDatabaseUtil.getDatabase().getReference().child("partners");

        recyclerView = (RecyclerView) view.findViewById(R.id.partner_store_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }
    @Override
    public void onStart() {
        super.onStart();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder>(
                PartnersList.class,
                R.layout.drawer_partner_store_list,
                PartnerStoreViewHolder.class,
                mRef
        ){
            @Override
            protected void populateViewHolder(PartnerStoreViewHolder viewHolder, PartnersList model, int position) {
                viewHolder.setStore(model.getPartnerStore());
                viewHolder.setDescription(model.getDescription());
                viewHolder.setImage(getActivity() ,model.getImage());
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        firebaseRecyclerAdapter.cleanup();
    }
}

感谢..

更新
我的问题是我从未使用过空间在我的Texview中,虽然我仍然不知道为什么它改变了维度。好吧,我在我的textview上作弊,我添加了空格来使用我的textview中未使用的间隔我实现它就像这样...... ...

public void setDescription(String description){
        TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
        if (description.length() <= 30){
            for (int x=0; x <= 99; x++){
                description = description + " "; //adding white spaces
            }
            descName.setText(description);
        }else {
            descName.setText(description);
        }
    }

感谢所有有帮助的人......我很感激

2 个答案:

答案 0 :(得分:1)

您仅将第三部分视图提供给textview,另外两部分提供给imageview的父布局,因为imageview宽度已修复,因此未使用该布局。你不应该使用重量,因为你给imageview提供固定的宽度和高度,并为textview包装内容。

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/partner_post_image"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_margin="8dp" />


    <LinearLayout
        android:weightSum="3"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/partner_store"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:gravity="bottom|start"
            android:text="Store Name"
            android:textAlignment="viewStart"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/partner_post_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="2"
            android:gravity="top|start"
            android:text="Description Here"
            android:textAlignment="viewStart"
            android:textColor="#000"
            android:textSize="12sp" />

    </LinearLayout>
</LinearLayout>

然后尝试

Picasso.with(context)
    .load(image)
    .error(android.R.drawable.stat_notify_error)
    .placeholder(R.drawable.ic_blu_logo)
    .fit()
    .centerCrop()
    .into(postImage);

如果您仍有任何问题,请与我们联系。

答案 1 :(得分:0)

尝试这种方法。

Picasso.with(context)
        .load(image)
        .error(android.R.drawable.stat_notify_error)
        .placeholder(R.drawable.ic_blu_logo)
        .fit()
        .centerCrop()
        .into(postImage);

在xml ImageView中使用android:scaleType="fitXY"。 你在ImageView中尝试了android:layout_width="match_parent"吗?