andriod studio adapter怎么实现

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>


我将外围/LinearLayout写进去,完了只是黑屏推出,是不是要将所有/LinearLayout的组件全写进适配器呀?

4个回答

llx2007
凌云志轩 没看懂呀
4 年多之前 回复

问题描述清楚些么,没看懂楼主啥意思

llx2007
凌云志轩 gridview里面规定了一行只显示一个item,每个item显示上面这个xml文件整体,但是xml文件里面的这个linearLayout高度要一样
4 年多之前 回复
llx2007
凌云志轩 main.xml中定义了一个gridview,我要将上面这个xml布局文件丢进这个gridview里面,但是布局不能变(每一行显示的高度一样)。现在是怎么进去合适?
4 年多之前 回复

mainactivity.java
public class MainActivity extends Activity {

 private LayoutInflater inflater;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);
    }

}

MyAdapter.java
public class MyAdapter extends BaseAdapter {
      private LayoutInflater inflater;
    private Context mcontext;
    private Integer[] mThumbnails = {R.drawable.unpress9};
    public MyAdapter( Context c) 
    { 
       mcontext = c;
    } 
     public int getCount() {
            return mThumbnails.length;
     }
     public Object getItem(int position) {
            return null;
        }

      public long getItemId(int position) {
            return 0;
        }


      @SuppressLint("InflateParams")
    public View getView(int position, View grid, ViewGroup parent) 
      {
          inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(grid != null)
     {
        inflater = LayoutInflater.from(mcontext);
        grid =inflater.inflate(R.layout.item_9,parent);
     }

     return grid;

     }

} 



item_9.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal">
    <LinearLayout
          android:orientation="horizontal">
        <LinearLayout
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text1"/>
            <ImageView
                android:id="@+id/img3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img4" />
            <TextView
                android:id="@+id/text2"/>
            <ProgressBar
                android:id="@+id/bar"  />
            <TextView
                android:id="@+id/text3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img" />
            <TextView
                android:id="@+id/text4" />
            <ProgressBar
                android:id="@+id/br" />
            <TextView
                android:id="@+id/text5"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img94"/>
            <TextView
                android:id="@+id/text6"/>
            <ProgressBar
                android:id="@+id/r" />
            <TextView
                android:id="@+id/text7"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img93" />
            <TextView
                android:id="@+id/text8"/>
            <ProgressBar
                android:id="@+id/ar" />
            <TextView
                android:id="@+id/text9"/>
        </LinearLayout>
        <LinearLayout
           android:orientation="horizontal">
            <ImageView
                android:id="@+id/img22" />
            <TextView
                android:id="@+id/text10"/>
            <ProgressBar
                android:id="@+id/ar13" />
            <TextView
                android:id="@+id/text11"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img6"/>
            <TextView
                android:id="@+id/text12" />
            <ProgressBar
                android:id="@+id/r17" />
            <TextView
                android:id="@+id/text13"/>
        </LinearLayout>       
         <LinearLayout
            android:orientation="horizontal" >
            <LinearLayout
                android:orientation="vertical" >
                <LinearLayout
                   android:orientation="horizontal" >
                    <TextView
                        android:id="@+id/text17"/>
                    <ImageView
                        android:id="@+id/mg"/>
                    <ImageView
                        android:id="@+id/mg2"/>
                    <ImageView
                        android:id="@+id/dev_ctrl_stat_img3"/>
                    <ImageView
                        android:id="@+id/k"/>
                    <ImageView
                        android:id="@+id/img90"/>
                    <ImageView
                        android:id="@+id/img91"/>
                    <TextView
                        android:id="@+id/text19"/>
                    <TextView
                        android:id="@+id/text20" />                  
                </LinearLayout>
                <LinearLayout
                    android:gravity="center_vertical" />
                    <ImageView                      
                        android:src="@drawable/img9" />
                    <TextView
                        android:gravity="center_vertical" />
                    <TextView
                        android:layout_width="wrap_content"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_weight="4.5" >
                <ImageView
                    android:id="@+id/qry"/>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

涨姿势了,关于不同子类型ListView还没想到Viewholder,学习了!!!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!