0

我有一个列表视图,我希望在左侧显示一些内容,在右侧显示其他内容: 例如

意思是内容

getItem(position).getIsMO() == false

显示在列表的左侧。以及其中的内容

getItem(position).getIsMO() == true

显示在列表的右侧。

该部分的代码是:

    public static class EfficientAdapter extends ArrayAdapter<Message>
            implements Filterable {
        private LayoutInflater mInflater;
        // private Bitmap mIcon1;
        private final Context context;
        private final ArrayList<Message> values;

        public EfficientAdapter(Context context, ArrayList<Message> values) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            super(context, R.layout.mycontent, values);
            mInflater = LayoutInflater.from(context);
            this.context = context;
            this.values = values;
        }

        /**
         * Make a view to hold each row.
         * 
         * @see android.widget.ListAdapter#getView1(int, android.view.View,
         *      android.view.ViewGroup)
         */
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid
            // unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;
            // When convertView is not null, we can reuse it directly, there is
            // no need
            // to reinflate it. We only inflate a new View when the convertView
            // supplied
            // by ListView is null.

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.mycontent, null);

                holder = new ViewHolder();
                if (getItem(position).getIsMO() == false) {
                    // parent.setPadding(0, 0, 15, 0);
                    // convertView.layout(15, 0, 0, 0);
                    holder.body = (TextView) convertView
                            .findViewById(R.id.body);
                    holder.date = (TextView) convertView
                            .findViewById(R.id.date);
                    holder.from = (TextView) convertView
                            .findViewById(R.id.from);
                    holder.status = (TextView) convertView
                            .findViewById(R.id.status);
                } else {
                    // convertView.layout(0, 0, 15, 0);
                    // parent.setPadding(15, 0, 0, 0);

                    holder.body = (TextView) convertView
                            .findViewById(R.id.body);
                    holder.date = (TextView) convertView
                            .findViewById(R.id.date);
                    holder.from = (TextView) convertView
                            .findViewById(R.id.from);
                    holder.status = (TextView) convertView
                            .findViewById(R.id.status);
                }
                convertView.setTag(holder);
            }

            else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.setOnClickListener(new OnClickListener() {
                private int pos = position;

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "Click-" + pos, Toast.LENGTH_SHORT)
                            .show();
                }
            });

            holder.body.setText(getItem(position).getBody());
            holder.date.setText(getItem(position).getDate());
            holder.from.setText(getItem(position).getPhoneNumber());
            holder.status
                    .setText(String.valueOf(getItem(position).getStatus()));
            /*
             * if ("000".equals(getItem(position).getPhoneNumber())) { //
             * convertView.setPadding(5, 0, 0, 0); // View layout =
             * View.inflate(context, R.layout.mycontent, null); //
             * layout.setPadding(5, 0, 0, 0);
             * convertView.setLayoutParams(params); // parent.setPadding(5, 0,
             * 0, 0); } else { // View layout = View.inflate(context,
             * R.layout.mycontent, null); // layout.setPadding(0, 0, 5, 0);
             * parent.setPadding(0, 0, 5, 0); // convertView.setPadding(0, 0, 5,
             * 0); }
             */
            return convertView;

        }

        static class ViewHolder {
            TextView from;
            TextView date;
            TextView body;
            TextView status;
        }
}

问题是,当我尝试parent.setPadding()所有对象在一侧的列表视图中对齐时。

感谢您抽出宝贵时间

我的内容1.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/objectscreenmoall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/content"
    android:clickable="true"
    android:onClick="btnListener"
    android:orientation="vertical"
    android:paddingRight="20dp"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/fromMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/phoNum"
            android:textColor="#01acec"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/dateMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/date"
            android:textColor="#01acec"
            android:textSize="15dp"
            android:layout_marginRight="10dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/bodyMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/body"
            android:textColor="#01acec"
            android:textSize="15dp"
            android:layout_marginLeft="7dp" />

        <TextView
            android:id="@+id/statusMO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:drawableRight="@drawable/ringtone_"
            android:layout_marginRight="10dp"
             />
    </RelativeLayout>

</LinearLayout>

列表视图.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/allList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listall"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="2"
        android:divider="#000000"
        android:dividerHeight="2dp"
        android:footerDividersEnabled="true"
         >
    </ListView>

</LinearLayout>
4

2 回答 2

1

您可以在此处拥有两个不同的 XML。

那是当getItem(position).getIsMO() == false设置第一个布局时:

convertView = mInflater.inflate(R.layout.mycontent1, null); // the row with blue background.

别的

convertView = mInflater.inflate(R.layout.mycontent2, null); // the row with green background.

尝试这样做。

如果这不起作用,我们会找到其他解决方案。

于 2012-07-24T13:27:02.180 回答
0

我想我会使用两种不同的行布局,一种android:layout_gravity="right"带有android:layout_gravity="left". 然后只需检查您需要在适配器中使用哪个。

于 2012-07-24T13:28:35.167 回答