如何在列表视图中每行右侧的4个图标

时间:2012-09-04 11:56:00

标签: android

我想在列表视图的每一行中向右侧设置3个图标。我正在制作购物应用程序,我想在列表视图中用户选择任何产品,这样他/她就可以像3用户一样看到产品将选择图标1,以便用户可以在网格视图中看到产品,如果用户选择图标2,那么用户可以在图像切换器中看到产品,在图标3上用户可以看到列表视图中的产品实际上我看到用户colud在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;}

}

3 个答案:

答案 0 :(得分:1)

为列表项行定义并使用xml布局文件。该行将在右侧包含4个 ImageViews 以及任何其他视图。并将其用作每行的布局。根据{{​​1}}课程中的位置,您可以获得对每一行的引用。

现在,为 CustomAdapter CustomAdapter中的列表项行充气此xml布局。

答案 1 :(得分:0)

你必须为你的列表视图创建custm视图,你可以简单地为它创建一个xml文件,稍后在Adapter类中为它充气。

就像这里我创建了一个带有两个TextView的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;

        }

答案 2 :(得分: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;
    }
}