How to inflate a ListView to a View

2019-08-08 15:27发布

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

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.

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

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

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

Edit: 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.

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>

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.

2条回答
够拽才男人
2楼-- · 2019-08-08 16:15

In this case what you can do is, create a empty layout in your main activity xml. Set a ID for that. While inflating the listview, inflate it to that layout.

In activity_main.xml include a relative layout as follows. Your parent layout is relative_layout. Add another layout like this.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/login_button" > //make sure you use this property to tell where you want to place the view in the screen. This should be below your last button.
</RelativeLayout>

Since login_button is last button in your view, i have recommended,

android:layout_below="@+id/login_button"
查看更多
forever°为你锁心
3楼-- · 2019-08-08 16:27

Why are you using View instead of ListView in your 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"/>

by

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

In your adapter replace

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

by

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

No need to cast it.

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

Try this it will surely work.

查看更多
登录 后发表回答