2

我想在列表视图的每一行的右侧设置 3 个图标。我正在制作购物应用程序,当用户在列表视图中选择任何产品时,我想在其中选择任何产品,这样他/她就可以以 3 种方式查看产品,就像用户选择图标一样1 所以用户可以在网格视图中看到产品,如果用户选择图标 2 所以用户可以在图像切换器中看到产品,在图标 3 上用户可以在列表视图中看到产品实际上我希望用户在 3 视图中看到产品。当点击图标时下一个活动打开就像用户选择网格视图的图标一样,所以下一个活动将打开网格视图。这是我的代码

public class ListViewSupplementActivityActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.listlayout);
ListView view = (ListView) findViewById(R.id.list);
view.setAdapter(new CustomImageListAapter(this));

view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if(position ==0)
{    
Intent ii = new Intent(ListViewSupplementActivityActivity.this,SingleListItem.class);

ii.putExtra("operation", position);
startActivity(ii);                                                           
}
if(position ==1){    
Intent in = new Intent(ListViewSupplementActivityActivity.this,Test.class);

in.putExtra("operation", position);
startActivity(in);
}
if(position == 5){
Intent in2= new               Intent(ListViewSupplementActivityActivity.this,FullImageActivity.class);       
Bundle bundle = new Bundle();
bundle.putInt("operation", position);
in2.putExtras(bundle);   
startActivity(buse); }

}  


});
}
}

这是自定义适配器类

public class CustomImageListAapter extends BaseAdapter {

private int[] images = {R.drawable.autumnwalk, R.drawable.bridge,R.drawable.butterfly,               
R.drawable.cheetah, R.drawable.cloud, R.drawable.su, R.drawable.wi,
R.drawable.rocksculpture, R.drawable.skyatsunset, R.drawable.s,
R.drawable.smoke, R.drawable.tulips};

private String[] imageDesc = { "Autumn Walk", "Bridge",
"Butterfly", "Cheetah", "Cloud", "Highway", "Martini", 
"Rock Sculpture", "Sky at Sunset", "Sliced Orange", "Smoke",
"Tulips"};

private Context ctx = null;

public CustomImageListAapter(Context context) {
this.ctx = context;
}

public int getCount() {
return images.length;
}

public Object getItem(int arg0) {
return null;
}

public long getItemId(int position) {
return 0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView imgView = new ImageView(this.ctx);
imgView.setScaleType(ScaleType.FIT_CENTER);
imgView.setPadding(8, 8, 8, 8);
imgView.setImageResource(images[arg0]);
imgView.setAdjustViewBounds(Boolean.TRUE);
imgView.setContentDescription(imageDesc[arg0]);
imgView.setMaxHeight(200);
imgView.setMaxWidth(200);

TextView tv = new TextView(this.ctx);
tv.setText(imageDesc[arg0]);
tv.setMaxHeight(100);
tv.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tv.setGravity(Gravity.CENTER);


LinearLayout layoutView = new LinearLayout(this.ctx);
layoutView.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 150);
layoutView.addView(imgView, params1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutView.addView(tv, params2);

return layoutView;}

}
4

3 回答 3

1

为列表项行定义和使用 xml 布局文件。该行将在右侧包含 4 个ImageView以及任何其他视图。并将其用作每一行的布局。并且根据您在CustomAdapter班级中的位置,您可以获得对每一行的引用。

getView()现在,为CustomAdapter中的列表项行扩展此 xml 布局。

于 2012-09-04T12:01:24.883 回答
0

为了节省以编程方式执行此操作的时间,您可以执行以下操作:

  • 创建 list_view_item xml文件(代表每个视图项)。要进行灵活对齐,您应该使用RelativeLayout.
  • 在您的数组适配器中扩展上述 xml 布局

(1)list_view_item.xml为项目布局创建:

  • 关键工作是将第一项设置为父视图的右侧:android:layout_alignParentRight="true"

  • 然后以下各项按上一项对齐:android:layout_alignParentLeft="@+id/id_of_prev_item"

(已编辑 3 张图片:)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="@+id/img1" />

    <ImageView
        android:id="@+id/img3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="@+id/img2" />

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="@+id/img3"
        android:fadingEdge="vertical" />

</RelativeLayout>

(2) 膨胀项目布局CustomImageListAapter

建议:将您的CustomImageListAapter类从ArrayAdapter<T>更具体的数据项使用扩展,并使您的代码更全面。

public class CustomImageListAapter extend ArrayAdapter<YourDataItem>
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        int item_view_id = R.layout.list_view_item;

        LinearLayout holderView = new LinearLayout(getContext());
        String inflaterName = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(inflaterName );
        inflater.inflate(item_view_id, holderView, true);

        //YourDataItem: is your class represent each row on your database
        YourDataItem item = getItem(position);

        //TODO: do your stuff of works with your view item and data here

        return holderView;
    }
}
于 2012-09-04T12:51:46.303 回答
0

您必须为列表查看器创建自定义视图,或者您可以简单地为此创建一个 xml 文件,然后在 Adapter 类中对其进行膨胀。

像这里我已经创建了一个带有两个 TextViews 的 xml 文件。

public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

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

                holder = new ViewHolder();

                holder.text1 = (TextView) convertView

                .findViewById(R.id.TextView01);

                holder.text2 = (TextView) convertView

                .findViewById(R.id.TextView02);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();

            }

            holder.text1.setText(CountriesList.abbreviations[position]);

            holder.text2.setText(CountriesList.countries[position]);

            return convertView;

        }
于 2012-09-04T12:06:33.490 回答