ListView is showing gapes in list items Android

醉酒当歌 提交于 2020-02-04 04:54:27

问题


i am trying to implement listView through setListAdapter and Efficient Adapter. I want that when list is show then the background should not be repeat. My code is repeating the whole layout of list.xml due to which my list item are showing with so much gap.

Right now my list is working like that:

But i want this type of view:

Here is my editText.xml in which i type the word and a list View is opened.

<EditText
    android:id="@+id/start_edit"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="top|left"
    android:ems="10"
    android:hint="Type to search"
    android:paddingLeft="50dp" >

    <requestFocus />
</EditText>

this layout is for list.xml :

<RelativeLayout
    android:id="@+id/RelativeLayout_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/blue_cellbar" >  

the list.xml file is repeating the layout in Effecient adapter:

here is my code:

listAdapter = new EfficientAdapter2(this);
    setListAdapter(listAdapter);



public static class viewHolder2 {
    TextView word;
    TextView meaning;
    ImageView image;
    ImageView image_color;
    RelativeLayout cell;

}



private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {

    private Context context;
    LayoutInflater inflater;

    public EfficientAdapter2(Context context) {

        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    public int getCount() {


        //  if(SearchWordString.isEmpty()==false)
        //  {

        return SearchWordString.size();
        /// }

        //return 0;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {

        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder2 holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list, null);

            ViewToUse=parent;
            holder = new viewHolder2();
            //  Log.i("View","is Null");
            convertView.setTag(holder);

        } else {

            //Log.i("View","is not Null");
            holder = (viewHolder2) convertView.getTag();
        }


        holder.cell = (RelativeLayout) convertView
                .findViewById(R.id.RelativeLayout_list);

        return convertView;
    }

UPDATE

public class Start extends ListActivity implements OnTouchListener,

android.view.GestureDetector.OnGestureListener {

// //////////////////////////////////////////////////// /************* INNER CLASS VIEWHOLDER ****************/ // //////////////////////////////////////////////////// onCreate() { ListView list_to_use = getListView(); listAdapter = new EfficientAdapter2(this); list_to_use.setAdapter(listAdapter); list_to_use.setBackgroundColor(2); viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

    search = (EditText) findViewById(R.id.start_edit);

    search.addTextChangedListener(myTextWatcher);

} public static class viewHolder2 { TextView word; TextView meaning; ImageView image; ImageView image_color; RelativeLayout cell;

}

// ////////////////////////////////////////////////////
/*********** INNER CLASS EfficientAdapter ************/
// ////////////////////////////////////////////////////

private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {

    private Context context;
    LayoutInflater inflater;

    public EfficientAdapter2(Context context) {

        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    public int getCount() {


        //  if(SearchWordString.isEmpty()==false)
        //  {

        return SearchWordString.size();
        /// }

        //return 0;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {

        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder2 holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_search_item, null);

            ViewToUse=parent;
            holder = new viewHolder2();
            //  Log.i("View","is Null");
            convertView.setTag(holder);

            holder.word = (TextView) convertView.findViewById(R.id.title_list);
            holder.meaning = (TextView) convertView
                    .findViewById(R.id.textView_meaning_list);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.image_list);
            holder.image_color = (ImageView) convertView
                    .findViewById(R.id.imageView_color_list);
            holder.cell = (RelativeLayout) convertView
                    .findViewById(R.id.RelativeLayout_list);

        } else {

            //Log.i("View","is not Null");

            holder = (viewHolder2) convertView.getTag();
        }

} }

main.xml:

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/start_edit"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="top|left"
    android:ems="10"
    android:hint="Type to search"
    android:paddingLeft="50dp" >

    <requestFocus />
</EditText>

<ViewFlipper
    android:id="@+id/viewFlipper1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top|left"
    android:layout_marginTop="50dp" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/blue_home"
            android:fastScrollEnabled="true"
            android:smoothScrollbar="true"
            android:divider="@drawable/blue_dic"
            android:dividerHeight="250sp" >

        </ListView>

    </FrameLayout>

</ViewFlipper>

rows for listView:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/image_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/abacus_thumbnail"
    android:scaleType="centerCrop" />

<ImageView
    android:id="@+id/imageView_color_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/blue_thumbnail" />

<TextView
    android:id="@+id/title_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/image_list"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/image_list"
    android:gravity="center"
    android:text="Abacus"
    android:textColor="#000000"
    android:textSize="30sp"
    android:textStyle="bold"
    android:typeface="sans" />

<TextView
    android:id="@+id/textView_meaning_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/title_list"
    android:layout_below="@+id/title_list"
    android:layout_marginTop="10dp"
    android:text="TextView"
    android:textColor="#000000"
    android:textSize="25sp" />


回答1:


I hope you have not provide android:dividerHeight attribute in your layout file under <ListView/> If so please remove it.




回答2:


this problem occurs when you have set the adapter class xml, parent root match_content instead of wrap_content. set the root content height wrap_content

 As i can see in adapter layout there is no parent root like relative, linearlayout, Framelayout and ConstraintLayout etc.


来源:https://stackoverflow.com/questions/14498163/listview-is-showing-gapes-in-list-items-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!