LayoutInflater.from.inflate的参数

学习ListView和Adpter时遇到的问题,记录一下

//News.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class News {
    private int aIcon;
    private String title;
    private String content;
}
//NewsAdapter.java
//mContext也可以不传,这里只在getView里使用,
//此时parent==NewsActivity的listview,parent.getContext==传来的NewsActivity对象
@AllArgsConstructor
public class NewsAdapter extends BaseAdapter {
    private List<News> data;
    private Context mContent;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	if(convertView==null){
        	convertView = LayoutInflater.from(mContent).inflate(R.layout.news_list_item, parent, false);
        }
        ImageView icon = convertView.findViewById(R.id.newsicon);
        TextView title = convertView.findViewById(R.id.newstitle);
        TextView content = convertView.findViewById(R.id.newscontent);
        News item = data.get(position);
        icon.setBackgroundResource(item.getAIcon());
        title.setText(item.getTitle());
        content.setText(item.getContent());
        return convertView;
    }
}
//list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/listview">
</ListView>
//news_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <ImageView
        android:id="@+id/newsicon"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:id="@+id/newstitle"
            android:layout_width="wrap_content"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:textSize="32dp"/>
        <TextView
            android:textSize="16dp"
            android:layout_weight="8"
            android:id="@+id/newscontent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
//NewsActivity.java
public class NewsActivity extends AppCompatActivity 
		implements AdapterView.OnItemClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        ListView listview = findViewById(R.id.listview);
        List<News> data=new ArrayList<News>();
        int id = getResources().getIdentifier("a1", "drawable", getPackageName());
        for(int i=0;i<14;i++){
            data.add(new News(id+i,"标题--"+i,"内容:"+i));
        }
        NewsAdapter newsAdapter = new NewsAdapter(data, this);
        listview.setAdapter(newsAdapter);
        listview.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,position+"点击",Toast.LENGTH_LONG).show();
    }
}

以上代码的效果如下:
LayoutInflater.from.inflate的参数_第1张图片
如果修改NewsAdapter.java的 LayoutInflater.from(mContent).inflate(R.layout.news_list_item, parent, false)成LayoutInflater.from(mContent).inflate(R.layout.news_list_item, null, false),效果如下:
LayoutInflater.from.inflate的参数_第2张图片

LayoutInflater.from(Context)

Obtains the LayoutInflater from the given context,从给定的Context获取LayoutInflater

inflate

Inflate a new view hierarchy from the specified xml resource,根据给定的resource生成层级的视图。
参数2非空且参数3为true,则把resource生成的视图作为参数2的子,并返回参数2;否则返回resource生成的视图。
参数3=false时,如果root不为null,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效。否则失效。

你可能感兴趣的:(android)