2012-09-04 35 views
2

我想在列表視圖的每一行中右側設置3個圖標。我正在製作購物應用程序,我想在列表視圖中選擇任何產品因此他/她可以用3種方式查看產品,如用戶將選擇圖標1,以便用戶可以在網格視圖中查看產品;如果用戶選擇圖標2,以便用戶可以在圖像切換器中看到產品,則圖標3用戶可以在列表視圖中查看產品其實我瓦納用戶colud看到產品在3 view.when點擊圖標,使下一個活動開放一樣,如果網格視圖,以便下一個活動的用戶選擇圖標就會打開電網view.this是我的代碼如何在列表視圖中的每一行右側的4個圖標

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;} 

} 

回答

1

爲列表項行定義並使用xml佈局文件。該行將包含右側的4 ImageViews以及任何其他視圖。並將其用作每一行的佈局。根據您在CustomAdapter類中的位置,您可以獲得對每個行的引用。

現在,將此xml版式的行列擴充爲getView()CustomAdapter

0

您必須爲列表視圖創建custm視圖,您可以簡單地爲該視圖創建一個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; 

     } 
0

爲了節省時間編程這樣做,你可以根據以下步驟做:

  • 創建XML LIST_VIEW_ITEM文件(代表每個視圖項)。要做到靈活對齊,你應該使用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; 
    } 
} 
+0

是否有必要CustomImageListAapter與ArrayAdapter延長?因爲我不是它與數據庫的連接。我會就現在我想簡單的這個任務。我是收到錯誤將其與ArrayAdapter –

+0

我所做的擴展數據庫信這在customImageListAdapter類中 –

+0

public View getView1(int position,View convertView,ViewGroup parent) int {item_view_id = R.layout.listlayout; LinearLayout holderView = new LinearLayout(getContext()); 字符串inflaterName = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater inflater =(LayoutInflater)getContext()。getSystemService(inflaterName); inflater.inflate(item_view_id,holderView,true); return holderView; } 私有上下文的getContext(){// TODO自動生成方法存根 返回NULL;} –

相關問題