如何将ListView充气到视图 [英] How to inflate a ListView to a View

查看:92
本文介绍了如何将ListView充气到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,所以我将尽我所能解释我的问题.

I'm new here so i will try to explain my problems as good as i can.

我试图在我的主要活动中将ListView充气到View中.我的主要活动在活动"的顶部有一些按钮和文本,并且listView还有足够的空间.列表视图由类别组成,分别表示为imageView和textview.

I am trying to inflate a ListView into a View in my main activity. My main activity has some buttons and texts on the top of the Activity and there is enough space left for the listView. The listview is consisted of categories, represented as an imageView and a textview.

我面临的问题是,当我为category列表创建的category_list_activity膨胀时,会发生两件事:

The problem im facing is that when i inflate the category_list_activity, the activity i created for the category list, two things happen:

ListView接管了所有屏幕,这意味着我既不能触摸按钮也不能触摸edittext,并且ListView也为空.

The ListView takes over all the screen, which means i cannot touch neither the buttons nor the edittext, and also the ListView is empty.

我创建了所需的适配器,并在stackof中搜索了一些信息,但找不到正确的答案.

I have created the Adapters needed and i have searched for some info here in stackof but i couldn't find any right answer.

编辑:由于解决了列表占据整个屏幕时的问题,我删除了不需要的部分代码. 解决方案是将已膨胀活动的(activity_category_list.xml)高度从"fill_parent"更改为"wrap_content".我还将显示代码限制为我认为没有加载类别的问题的部分.

due to solving the problem when the list was taking over the whole screen i remove the parts of code that is not needed. The solution was to change the inflated activity's (activity_category_list.xml) height from "fill_parent" to "wrap_content". I also restricted the show code to the parts i think there is the problem about not loading the categories.

这是我编写的部分代码: MainActivity.java

here is the parts of code i wrote: MainActivity.java

public class MainActivity extends ActionBarActivity {
Activity a;
Button toogle_button;
Button go_button;
Button login_button;
EditText search_text;
View inflating_view;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    a = this;

    inflating_view = findViewById(R.id.inflating_view);
    ViewGroup parent =(ViewGroup) inflating_view.getParent();
    int index = parent.indexOfChild(inflating_view);
    parent.removeView(inflating_view);
    inflating_view = getLayoutInflater().inflate(R.layout.activity_category_list, parent, false);
    parent.addView(inflating_view, index);
    inflating_view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast t = Toast.makeText(getApplicationContext(), "ListView clicked", Toast.LENGTH_SHORT);
            t.show();
        }
    });
 }   

CategoryAdapter.java

private class Viewholder{
    TextView category_text;
    ImageView image;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Viewholder holder = null;
    Category category = categories.get(position);

    if (convertView == null){

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = (View) inflater.inflate(R.layout.activity_category, null);

        holder = new Viewholder();
        holder.category_text = (TextView) convertView.findViewById(R.id.category_text);
        holder.image = (ImageView) convertView.findViewById(R.id.category_image);


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

    holder.category_text.setText(category.getName());
    holder.image.setImageURI(category.getImageUri());
    return convertView;
}
}

CategoryListActivity.java

public class CategoryListActivity extends ActionBarActivity {

    ListView category_list_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_category_list);

        Log.d("Category List View", "Category list view is called");
        category_list_view = (ListView) findViewById(R.id.category_list_view);
        CategoryAdapter ca = new CategoryAdapter(getApplicationContext(),
                TestValues.categories);

        category_list_view.setAdapter(ca);
        category_list_view.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast toast = Toast.makeText(getApplicationContext(),
                        "Clicked Category" + parent.getItemIdAtPosition(position), Toast.LENGTH_LONG);
                toast.show();

            }

        });
        Log.d("Category List View", "Everything is loaded");
    }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    style="@android:style/Theme.Black.NoTitleBar"
    tools:context="com.example.aggro.activities.MainActivity" >




    <Button
        android:id="@+id/toggle_button"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/toogle_list_en"
        android:textSize="10dp" 
        />

 <TextView 
        android:id="@+id/search_text_view"
        android:layout_toRightOf="@+id/toggle_button"
        android:layout_alignParentTop="true"
        android:text="@string/search_text_en"
        android:layout_height="30dp"
        android:paddingTop="7dp"
        android:textSize="10dp"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"/>

    <EditText
        android:id="@+id/search_text"
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:textColor="#FFFFFF"
        android:textSize="15dp"

        android:background="#000000"
        android:layout_toLeftOf="@+id/search_go"
        android:layout_toRightOf="@+id/search_text_view" />

    <Button
        android:id="@+id/search_go"
        android:layout_width="wrap_content"
        android:layout_height="30dp"

        android:layout_toLeftOf="@+id/login_button"
        android:layout_alignParentTop="true"
        android:text="@string/search_go_en"
        android:textSize="10dp" />



     <Button
        android:id="@+id/login_button"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/login_en"
        android:textSize="10dp" />

     <View
         android:id="@+id/inflating_view"
         android:layout_below="@+id/toggle_button"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"/>


</RelativeLayout>

我已经单独检查了CategoryListActivity.java,它可以按预期的方式工作,所以我认为适配器可以正常工作.

I have checked the CategoryListActivity.java alone and it works as it was supposed to, so i think the adapter works right.

如果您需要任何其他信息,请告诉我. 预先感谢.

If you need any other information please let me know. Thanks in advance.

推荐答案

为什么要在activity_main.xml中使用View而不是ListView?

Why are you using View instead of ListView in your activity_main.xml?

更改activity_main.xml的这一部分

Change this part of activity_main.xml

    <View
     android:id="@+id/inflating_view"
     android:layout_below="@+id/toggle_button"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>

作者

    <ListView 
      android:id="@+id/category_list_view"
      android:layout_below="@+id/toggle_button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

在适配器中替换

    convertView = (View) inflater.inflate(R.layout.activity_category, null);

作者

    convertView = inflater.inflate(R.layout.activity_category, null);

无需投射.

删除 activity_category_list.xml CategoryListActivity.java . 初始化和使用ListView& MainActivity.java

Remove activity_category_list.xml and CategoryListActivity.java. Initialize & use ListView & Adapter in MainActivity.java

尝试此操作一定可以.

这篇关于如何将ListView充气到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆