2013-10-31 32 views
0

我在這裏充氣佈局到另一個佈局這段代碼,我能夠膨脹的佈局具有列表視圖是否可以在多行上顯示圖像視圖和文本視圖而不使用android中的List視圖?我使用列表視圖至今

public class MyListAdapter extends ArrayAdapter<Certs>{ 

     Context context; 
     public MyListAdapter(Context context, int resourceId,List<Certs> items){ 
      super(context, resourceId, items); 
      this.context = context; 

     } 
     /*private view holder class*/ 
     public class ViewHolder { 
      ImageView imageView; 
      TextView makeText; 
      ProgressBar mPB;  
     } 


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      //ViewHolder holder = null; 



      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.item_view, null); 
       holder = new ViewHolder(); 

       holder.makeText = (TextView) convertView.findViewById(R.id.item_txtName); 
       holder.imageView = (ImageView) convertView.findViewById(R.id.item_icon); 
       holder.mPB = (ProgressBar) convertView.findViewById(R.id.progress); 
       //holder.mPB.getIndeterminateDrawable().setColorFilter(0xFF4b86bb, android.graphics.PorterDuff.Mode.DST); 

       convertView.setTag(holder); 
      } else{ 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      Certs currentCert = myCerts.get(position); 

      holder.makeText.setText(currentCert.getName().toString()); 
      Typeface face = Typeface.createFromAsset(getAssets(), "fonts/calligraphic.ttf"); 
      holder.makeText.setTypeface(face); 

      holder.imageView.setImageResource(currentCert.getIconID()); 


      //holder.mPB.setProgress(10); 
      holder.mPB.setId(currentCert.getPbID()); 

      return convertView; 
     } 


    } 

,但我不希望使用列表視圖想這兒過得不同行中的多個圖像和文本將顯示在相同的線性佈局上,而不使用列表視圖。

+0

您可以通過創建一個充氣佈局的自定義視圖加載圖像。點擊這裏http://rajeshvijayakumar.blogspot.in/2013/03/inflating-ui-dynammically-example-in.html – krishna

+0

如果ListView不可見,ListView有自己的分配和釋放視圖的屬性。如果你不使用這個並且使用你的線性佈局方案,你可能會停留在內存管理上。 –

+0

爲什麼不爲了你的目的改進你的item_view佈局? – Devrim

回答

0

是有可能做so.I創建相關的代碼像你...

package com.android.customlayout; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity implements OnClickListener { 

    private Button mAddButton; 
    private Button mDeleteButton; 
    private LinearLayout mLinear; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mAddButton = (Button) findViewById(R.id.add_button); 
     mDeleteButton = (Button) findViewById(R.id.delete_button); 
     mLinear = (LinearLayout) findViewById(R.id.child_linear); 
     mAddButton.setOnClickListener(this); 
     mDeleteButton.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 

     switch(v.getId()) { 
      case R.id.add_button : 
       View childView = getLayoutInflater().inflate(R.layout.custom_layout, null); 
       mLinear.addView(childView); 
       break; 
      case R.id.delete_button : 
       int childSize = mLinear.getChildCount(); 
       if(childSize != 0) { 
        mLinear.removeViewAt(childSize - 1); 
       } 
       break; 
     } 
    } 
} 

而在你的主XML:

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

     <Button 
      android:id="@+id/add_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_marginLeft="29dp" 
      android:text="Add" /> 

     <Button 
      android:id="@+id/delete_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="36dp" 
         android:layout_marginTop="10dp" 
      android:layout_toRightOf="@id/add_button" 
      android:text="Delete" /> 

     <LinearLayout android:id="@+id/child_linear" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/add_button" 
      android:layout_marginTop="45dp" 
      android:orientation="vertical" > 
     </LinearLayout> 

    </RelativeLayout> 

再拍XML定製命名Layout.xml

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

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" /> 

</LinearLayout> 

希望這將有助於.. :)

+0

Thanx爲您的答案,但我問的圖像視圖與文字視圖,可以滾動不使用列表視圖,因爲我能夠顯示不同的字符串在不同的行內一個滾動視圖,但當我用來顯示不同的圖像在每一行我都未能實現this.so請幫助我與您的相關建議。謝謝 –

+0

我必須使用我的自定義適配器動態設置字符串和圖像。 –

+0

Arjun你的問題是圖像沒有在列表視圖中顯示。我是對的?。 –

相關問題