添加空格以使每个列表项更大

时间:2018-02-07 15:32:50

标签: java android xml listview textview

我正在创建的应用有两个列表视图。我的第二个listview与我的第二个listview看起来不一样。分隔每个项目的行太靠近文本,但我希望它们在顶部和底部都有空格,就像第一个列表一样。我试图改变填充和边距,但这不起作用,我也尝试在其中一个Java类中更改它,但我收到错误消息。

Here are two pictures for comparison(The first list is on the left and the second list is on the right)

第二个清单:

<ListView

    android:id="@+id/countryselector"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_above="@+id/checkItem"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="60dp"
    android:clickable="false"/>

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


<TextView
    android:id="@+id/emptyElement"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="NO ITEM AVAILABLE!"
    android:textColor="#525252"
    android:textSize="19.0sp"
    android:visibility="gone" />

<Button
    android:id="@+id/checklist"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentTop="true"
    android:gravity="end"
    android:layout_alignParentLeft="true"
    android:text=""
    android:background="@mipmap/arrow2"

我的第一个清单:

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="left"
    android:text="Favorites"
    android:textColor="#222"
    android:textSize="15dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="2dp"
    android:textStyle="normal" />

<Button
    android:id="@+id/tofavorites"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentTop="true"
    android:gravity="end"
    android:layout_alignParentLeft="true"
    android:text=""
    android:background="@mipmap/arrow2" />


<ListView
    android:layout_marginTop="60dp"
    android:id="@+id/countrieselector"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:clickable="false" />

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

这是我的第二个列表的适配器类:

public class AdapterFavorite extends BaseAdapter {

    ArrayList<String> items;
    ArrayList<String> items1;
    LayoutInflater inflater;
    ArrayList selectedItems;
    List<Item> dummyItems;
    ArrayList<Integer> itemsValue = new ArrayList<>();
    Activity activity;
    DbUtility dbUtility;
    Context context;


    AdapterFavorite(Activity activity, ArrayList<String> items, Context context) {

        this.items = items;
        inflater = activity.getLayoutInflater();
        selectedItems = new ArrayList<String>();
        this.activity = activity;
        this.context = context;
        dbUtility = new DbUtility(activity);


    }

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

    @Override
    public String getItem(int i) {
        return items.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        dbUtility.open();
        View v = view;
        if (v == null) {
            v = inflater.inflate(R.layout.checkboxlayoutfavorite, null);
        }



        TextView textview_countries = (TextView) v.findViewById(R.id.Textview_languages);
        Button button = (Button) v.findViewById(R.id.checkbox);

        items1 = new ArrayList<>();

        textview_countries.setText(items.get(i));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ////Where I tried to add/move items when the checkbox is clicked

                dbUtility.delete_byID(items.get(i));
                items.remove(i);
                notifyDataSetChanged();

            }
        });



        return v;
    }


}

这是我的第二个listL的主要Java类:

ublic class FavoriteActivity extends AppCompatActivity {
    DbUtility dbUtility;
    List<Item> dummyItems = new ArrayList<>();
    Button mainList;
    KeyboardPrefManager keyboardPrefManager;
    ArrayList<String> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ListView chl = (ListView) findViewById(R.id.languageselector);
        mainList = (Button) findViewById(R.id.checklist);
        chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        items = new ArrayList<>();
        dbUtility = new DbUtility(this);
        dbUtility.open();
        dummyItems = dbUtility.getAllContacts();

        keyboardPrefManager = new KeyboardPrefManager(this);

        for (int a = 0; a < dummyItems.size(); a++) {
            items.add(dummyItems.get(a).getValue());
        }




        mainList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FavoriteActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        AdapterFavorite adapter = new AdapterFavorite(this, items, this);
        chl.setAdapter(adapter);
        chl.setEmptyView(findViewById(R.id.emptyElement));
        chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String item = items.get(i); // Getting the dummy item from the List with position i
                Toast.makeText(FavoriteActivity.this, "Clicked on " + item, Toast.LENGTH_SHORT).show();
                keyboardPrefManager.selectKeyboard(item);
            }
        });}}

1 个答案:

答案 0 :(得分:0)

请检查您的列表项xml以获取相同或不同的列表。谢谢