列表视图中的箭头图标

时间:2012-10-15 11:27:38

标签: android android-layout android-intent

我是android的新手。我正在使用包含图像和textview的listview。我想在每一行中添加箭头图标。我正在尝试但我无法完成此操作。我怎么能这样做?代码如下。非常感谢任何帮助。

CategoryActivity.java

public class CategoryActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.category1);


    ImageView in1 = (ImageView) findViewById(R.id.glrrr1);
     in1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             Intent myIntent = new Intent(view.getContext(),ImageSwitcherA.class);
             startActivityForResult(myIntent, 0);
         }

     });


     ImageView in2 = (ImageView) findViewById(R.id.grid1);
    in2.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             Intent myIntent1 = new Intent(view.getContext(),RingGridView.class);
             startActivityForResult(myIntent1, 0);
         }

     });

    ListView view = (ListView) findViewById(R.id.list);
    //instance of custom adapter
    view.setAdapter(new CustomImageListAapter(this));

    view.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int     position, long id) {

           //Intent i = new    Intent(AndroidGridLayoutActivity.this,FullImageActivity.class);
               //Bundle bundle = new Bundle();
              // bundle.putInt("operation", position);
               //i.putExtras(bundle);
              //startActivity(i);

               if(position ==0)
               {    
                     Intent ii = new Intent(CategoryActivity.this,Ring1Full.class);
                    //Bundle bundle = new Bundle();
                   //bundle.putInt("operation", position);
                    ii.putExtra("operation", position);
                     startActivity(ii);

               }

               if(position ==1)
               {    
                     Intent in = new Intent(CategoryActivity.this,Ring2Full.class);
                    //Bundle bundle = new Bundle();
                  // bundle.putInt("operation", position);
                    in.putExtra("operation", position);
                     startActivity(in);

               }
               if(position ==2)
               {    
                     Intent inn = new Intent(CategoryActivity.this,Ring3Full.class);
                    //Bundle bundle = new Bundle();
                   //bundle.putInt("operation", position);
                    inn.putExtra("operation", position);
                     startActivity(inn);

               }
               if(position ==3)
               {    
                     Intent innm = new Intent(CategoryActivity.this,Ring4Full.class);
                    //Bundle bundle = new Bundle();
                   //bundle.putInt("operation", position);
                    innm.putExtra("operation", position);
                     startActivity(innm);

               }
               if(position ==4)
               {    
                     Intent intt = new Intent(CategoryActivity.this,Ring5Full.class);
                    //Bundle bundle = new Bundle();
                  // bundle.putInt("operation", position);
                    intt.putExtra("operation", position);
                     startActivity(intt);

               }
               if(position ==5)
               {    
                     Intent intt5 = new Intent(CategoryActivity.this,Ring6Full.class);
                    //Bundle bundle = new Bundle();
                   //bundle.putInt("operation", position);
                    intt5.putExtra("operation", position);
                     startActivity(intt5);

               }
               if(position == 6)
               {

                          Intent buset= new Intent(CategoryActivity.this,Ring7Full.class);
                         // Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       buset.putExtra("operation", position);   
                          startActivity(buset);


               }

               if(position == 7)
               {

                          Intent buses= new Intent(CategoryActivity.this,Ring8Full.class);
                          //Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       buses.putExtra("operation", position);   
                          startActivity(buses);



   }
               if(position == 8)
               {

                          Intent busez= new Intent(CategoryActivity.this,Ring9Full.class);
                          //Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       busez.putExtra("operation", position);   
                          startActivity(busez);


               }
               if(position == 9)
               {

                          Intent buseh= new Intent(CategoryActivity.this,Ring10Full.class);
                          //Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       buseh.putExtra("operation", position);   
                          startActivity(buseh);


               }
               if(position == 10)
               {

                          Intent busek1= new Intent(CategoryActivity.this,Ring11Full.class);
                          //Bundle bundle = new Bundle();
                      //bundle.putInt("operation", position);
                       busek1.putExtra("operation", position);   
                          startActivity(busek1);


               }

               if(position == 11)
               {

                          Intent busek2= new Intent(CategoryActivity.this,Ring12Full.class);
                          //Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       busek2.putExtra("operation", position);   
                          startActivity(busek2);


               }
               if(position == 12)
               {

                          Intent busek3= new Intent(CategoryActivity.this,Ring13Full.class);
                          //Bundle bundle = new Bundle();
                      // bundle.putInt("operation", position);
                       busek3.putExtra("operation", position);   
                          startActivity(busek3);


               }
               if(position == 13)
               {

                          Intent busek4= new Intent(CategoryActivity.this,Ring14Full.class);
                          //Bundle bundle = new Bundle();
                      // bundle.putInt("operation", position);
                       busek4.putExtra("operation", position);   
                          startActivity(busek4);


               }
               if(position == 14)
               {

                          Intent busek5= new Intent(CategoryActivity.this,Ring16Full.class);
                         // Bundle bundle = new Bundle();
                      // bundle.putInt("operation", position);
                       busek5.putExtra("operation", position);   
                          startActivity(busek5);


               }
               if(position == 15)
               {

                          Intent busek6= new Intent(CategoryActivity.this,Ring16Full.class);
                          //Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       busek6.putExtra("operation", position);   
                          startActivity(busek6);


               }
               if(position == 16)
               {

                          Intent busek7= new Intent(CategoryActivity.this,Ring17Full.class);
                         // Bundle bundle = new Bundle();
                       //bundle.putInt("operation", position);
                       busek7.putExtra("operation", position);   
                          startActivity(busek7);


               }
               if(position == 17)
               {

                          Intent busek8= new Intent(CategoryActivity.this,Ring18Full.class);
                          //Bundle bundle = new Bundle();
                      //bundle.putInt("operation", position);
                       busek8.putExtra("operation", position);   
                          startActivity(busek8);


               }





           }
        });}}

CustomImageListAapter.java

  public class CustomImageListAapter extends BaseAdapter {

private int[] images = {
        R.drawable.rrr1,  
        R.drawable.rrr2,  
        R.drawable.rrr3,  
        R.drawable.rrr4,  
        R.drawable.rrr5, 
        R.drawable.rrr6, 
        R.drawable.rrr7, 
        R.drawable.rrr8,
        R.drawable.rrr18, 
        R.drawable.rrr10,
        R.drawable.rrr11, 
        R.drawable.rrr12,
        R.drawable.rrr13,
        R.drawable.rrr14,       
        R.drawable.rrr15,
        R.drawable.rrr16,
        R.drawable.rrr17, 
        R.drawable.rrr18, 

};

private String[] imageDesc = { "Diamond Ring", "Silver Ring",
        "Gold Ring","Antique Ring","Pearl Ring","Beats Ring","Diamond Ring","Stone Ring","Antique Ring","Diamond Ring", "Silver Ring",
        "Gold Ring","Antique Ring","Pearl Ring","Beats Ring","Diamond Ring","Stone Ring","Antique Ring"};
        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;
}

}

的xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:id="@+id/background"
 android:gravity="center_vertical"
android:orientation="vertical" >

<ListView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/list"


android:background="@drawable/bkg">

</ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

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(
                   150 ,LayoutParams.WRAP_CONTENT);
    layoutView.addView(tv, params2);
ImageView i1= new ImageView(this.ctx);
         i1.setImageResource(R.drawable.ic_launcher);
layoutView.addView(i1);
return layoutView;

尝试获取视图将一个imageview添加到该布局(layoutView)

答案 1 :(得分:0)

首先,您必须为ListView的行定义自定义布局。以下代码左侧有ImageView,右侧有标题和子标题,右侧有箭头图标。

<强> ListRow.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5sp" > 

    <!--  Left side Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5sp"       
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg" 
        android:layout_marginRight="5sp">

        <ImageView
            android:id="@+id/list_image"   
             android:layout_width="50sp"
            android:layout_height="50sp"
            />

    </LinearLayout>

    <!-- Title-->
    <TextView
        android:id="@+id/title_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textStyle="bold"/>

    <!-- Subtitle -->
    <TextView
        android:id="@+id/subtitle_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_name"
        android:layout_marginTop="2sp"
        android:layout_toRightOf="@+id/thumbnail"
        />

     <!-- Rightend Arrow -->    
     <ImageView android:contentDescription="@string/right_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

现在,您必须创建一个自定义Adapter来显示自定义ListView,这会使ListRow.xml膨胀并显示列表中的数据。您还可以访问以下链接来创建布局: -

android-custom-listview-with-image-and-text

希望这会有所帮助。感谢。