回收视图项目组件重叠

时间:2017-07-28 07:25:29

标签: android android-layout android-recyclerview

  

在第二项中它似乎是重叠元素。这可能是由于文本较长但我无法修复它。   fragment_meds.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="175dp"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="0.35"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:orientation="vertical">
        <ImageView

            android:background="@drawable/default_meds_small"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.65"
        android:paddingTop="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/id"
            android:layout_gravity="left"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            />

        <TextView

            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            />
        <TextView
            android:drawableLeft="@drawable/resupply"
            android:id="@+id/resupply_button"
            android:drawablePadding="5dp"
            android:textColor="@color/black"
            android:text="@string/add_to_resupply"
            android:layout_width="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="40dp"
            android:layout_height="20dp" />
    </LinearLayout>
</LinearLayout>
  

AllMedicineRecyclerViewAdapter.java

public class AllMedicineRecyclerViewAdapter extends RecyclerView.Adapter<AllMedicineRecyclerViewAdapter.ViewHolder> {

    private final List<MedicineList> mValues;
    private final AllMedicineFragment.OnListFragmentInteractionListener mListener;
    Context context;
    DbHelper db;
    private List<com.allion.medimap.dto.Medicine> mMedicineData = new ArrayList<com.allion.medimap.dto.Medicine>();

    public AllMedicineRecyclerViewAdapter(List<MedicineList> items, AllMedicineFragment.OnListFragmentInteractionListener listener, Context context) {
        mValues = items;
        mListener = listener;
        this.context = context;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_meds, parent, false);
        db = new DbHelper(context);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText(mValues.get(position).getMedicine().getName());

        holder.resupply_button.setText(R.string.add_to_resupply);
        mMedicineData = db.getAllMedicine();
        Log.e("adpater", "start");
        Log.e("size", String.valueOf(mMedicineData.size()));


        if (mValues.get(position).getMedicine().getMed_id().equals("pack_med")) {
            if (SessionManager.getInstance(context).getPack()) {
                Log.e("test_add_pack", "add");

                holder.resupply_button.setText(R.string.added_to_resupply);
                holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);

            } else {
                Log.e("test_add_pack", "added");

                holder.resupply_button.setText(R.string.add_to_resupply);
                holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.resupply, 0, 0, 0);
            }
        } else {
            for (int i = 0; i < mMedicineData.size(); i++) {
                if(mValues.get(position).getMedicine().getCategory().equals("prn"))
                {
                    holder.resupply_button.setVisibility(View.GONE);
                }
                else {

                    if (mValues.get(position).getMedicine().getName().equals(mMedicineData.get(i).getName())) {
                        Log.e("true_name1", mMedicineData.get(i).getId());
                        Log.e("true_name2", mValues.get(position).getMedicine().getMed_id());

                        holder.resupply_button.setText(R.string.added_to_resupply);
                        holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);

                    }
                }
            }
        }


        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {

                    if (!mValues.get(position).getMedicine().getMed_id().equals("pack_med")) {
                        startMedicineDetailFromTimelineActivity(context, mValues.get(position).getMedicine());
                        mListener.onListFragmentInteraction(holder.mItem);
                    }
                }
            }
        });
        holder.resupply_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mValues.get(position).getMedicine().getMed_id().equals("pack_med")) {
                    if (SessionManager.getInstance(context).getPack()) {
                        Log.e("test_add_pack", "add");
                        SessionManager.getInstance(context)
                                .setPack(context, false);
                        holder.resupply_button.setText(R.string.add_to_resupply);
                        holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.resupply, 0, 0, 0);

                    } else {
                        Log.e("test_added_pack", "added");
                        SessionManager.getInstance(context)
                                .setPack(context, true);
                        holder.resupply_button.setText(R.string.added_to_resupply);
                        holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);
                    }

                } else {
                    if (holder.resupply_button.getText().toString().equals("Add To Resupply")) {

                        Log.e("test_added", "added");
                        com.allion.medimap.dto.Medicine medicine = new com.allion.medimap.dto.Medicine(mValues.get(position).getMedicine().getMed_id(), mValues.get(position).getMedicine().getName(), mValues.get(position).getMedicine().getCategory());
                        db.insertMedicineDetail(medicine);
                        holder.resupply_button.setText(R.string.added_to_resupply);
                        holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check, 0, 0, 0);
                    } else {
//
                        db.deleteRow(mValues.get(position).getMedicine().getMed_id());
                        holder.resupply_button.setText(R.string.add_to_resupply);
                        holder.resupply_button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.resupply, 0, 0, 0);
                        Log.e("test_add", "added");

//
                    }
                }

            }
        });
    }

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

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public final TextView resupply_button;
        public MedicineList mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.content);
            resupply_button = (TextView) view.findViewById(R.id.resupply_button);

        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }


    }


}
  

上面我添加了相关的代码,在这里我需要提一下我禁用了Recycle-view的项目可重用性功能。如果有人可以帮我解决这个问题,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

尝试这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="175dp"
android:orientation="horizontal">
<ImageView
    android:id="@+id/iv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:padding="10dp"
    android:src="@drawable/default_meds_small"/>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@+id/iv"
    android:paddingTop="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/id"
        android:layout_gravity="left"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        />

    <TextView

        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        />
    <TextView
        android:drawableLeft="@drawable/resupply"
        android:id="@+id/resupply_button"
        android:drawablePadding="5dp"
        android:textColor="@color/black"
        android:text="@string/add_to_resupply"
        android:layout_width="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="40dp"
        android:layout_height="20dp" />
</LinearLayout>
</RelativeLayout>

答案 1 :(得分:2)

不使用 LinearLayout 仅用于一个小部件,您可以直接使用Imageview

    <ImageView
    android:background="@drawable/default_meds_small"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:adjustViewBound="true"
    android:padding="10dp"
    android:layout_weight="0.35" />