用户可自定义的菜单项

时间:2018-01-13 23:40:13

标签: android android-settings

我无法在任何地方找到答案。我有一个包含24个项目的菜单。我希望用户能够使用“设置”选择要在菜单上显示的24个项目。我不能为此制定战略。有没有人有任何想法?我没有代码,因为我不知道从哪里开始,除了使用Setiings的基础知识。

我不确定为什么在没有解释的情况下将其拒之门外。问题太混乱了吗?

1 个答案:

答案 0 :(得分:0)

这是我在了解了ListView可以做的事情之后最终得到的:

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.triunedev.custommenucode.MainActivity">

    <ListView
        android:id="@+id/outOfMenuList"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginTop="10dp"
        android:choiceMode="multipleChoice"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <LinearLayout
        android:id="@+id/moveArrows"
        android:layout_width="0dp"
        android:layout_height="36dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/insideMenuList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/outOfMenuList">

        <ImageView
            android:id="@+id/addArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="100dp"
            android:layout_weight="2"
            android:contentDescription="@string/in_arrow"
            android:onClick="moveItem"
            android:visibility="visible"
            app:srcCompat="@drawable/arrow_down" />

        <ImageView
            android:id="@+id/removeArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="100dp"
            android:layout_weight="2"
            android:contentDescription="@string/out_arrow"
            android:onClick="moveItem"
            android:visibility="visible"
            app:srcCompat="@drawable/arrow_up" />

    </LinearLayout>

    <ListView
        android:id="@+id/insideMenuList"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:choiceMode="multipleChoice"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/moveArrows"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

JAVA

package com.triunedev.custommenucode;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import static java.util.Arrays.asList;

public class MainActivity extends AppCompatActivity {

    ListView insideMenuList;
    ListView outOfMenuList;
    ArrayList<String> insideMenuArray;
    ArrayList<String> outOfMenuArray;
    ArrayAdapter<String> outOfMenuAdapter;
    ArrayAdapter<String> insideMenuAdapter;
    int count;
    String listInFocus;
    String[] toMoveToMenuList;
    String[] toMoveOutOfMenuList = new String[24];

    public void buildListArrays() {

        insideMenuList = (ListView) findViewById(R.id.insideMenuList);
        outOfMenuList = (ListView) findViewById(R.id.outOfMenuList);

        insideMenuArray = new ArrayList<String>(asList(
                "Chick-fil-A",
                "In-N-Out Burger",
                "Wendys",
                "Dairy Queen",
                "Kentucky Fried Chicken",
                "Carl's Jr.",
                "McDonald's",
                "Sonic",
                "Jack-in-the-Box",
                "Taco Bell",
                "Burger King",
                "Arby's",
                "El Pollo Loco",
                "Popeye's",
                "Starbucks",
                "Panda Express",
                "Steak 'n Shake",
                "Jersey Mike's",
                "A&W",
                "Whitecastle",
                "Hardee's",
                "Del Taco",
                "Subway",
                "Long John Silver's"));

        outOfMenuArray = new ArrayList<String>(asList(
                "Auntie Anne's",
                "Cinnabon",
                "Dunkin' Donuts",
                "Pizza Hut",
                "Panera Bread",
                "Domino's",
                "Chipotle",
                "Little Caesars",
                "Papa John's",
                "Jimmy John's",
                "Zaxby's",
                "Culver's",
                "Bojangles'",
                "Church's Chicken",
                "Papa Murphy's",
                "Baskin-Robbins",
                "Jamba Juice",
                "Coldstone Creamery",
                "Five Guys",
                "Jollibee",
                "Krispy Kreme",
                "Hook Burger",
                "Quizno's",
                "Coffee Bean & Tea Leaf",
                "TCBY",
                "Tim Hortons",
                "Wingstop",
                "WingStreet"));

        populateListViews();

        insideMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                if(listInFocus.equals("outOfMenuList")) {
                    outOfMenuList.setAdapter(outOfMenuAdapter);
                    count = 0;
                }

                parent.getChildAt(position).setBackgroundColor(Color.argb(100, 255, 242, 99));
                toMoveOutOfMenuList[position] = insideMenuArray.get(position);
                count++;
                listInFocus = parent.getResources().getResourceEntryName(parent.getId());

            }
        });

        outOfMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                if(listInFocus.equals("insideMenuList")) {
                    insideMenuList.setAdapter(insideMenuAdapter);
                    count = 0;
                }

                parent.getChildAt(position).setBackgroundColor(Color.argb(100, 255, 242, 99));
                toMoveToMenuList[position] = outOfMenuArray.get(position);
                count++;
                listInFocus = parent.getResources().getResourceEntryName(parent.getId());

            }
        });

    }

    public void populateListViews() {

        toMoveToMenuList = new String[outOfMenuArray.size()];
        for(int i=0; i<outOfMenuArray.size(); i++) {
            toMoveToMenuList[i] = "empty";
        }

        for(int i=0; i<24; i++) {
            toMoveOutOfMenuList[i] = "empty";
        }

        insideMenuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, insideMenuArray);
        insideMenuList.setAdapter(insideMenuAdapter);
        outOfMenuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, outOfMenuArray);
        outOfMenuList.setAdapter(outOfMenuAdapter);
        listInFocus = "";
        count = 0;

    }

    public void moveItem(View view) {

        String id = view.getResources().getResourceEntryName(view.getId());
        if((id.equals("removeArrow") || id.equals("addArrow")) && listInFocus.equals("")) {
            Toast.makeText(getApplicationContext(), "Please choose an item to move.", Toast.LENGTH_LONG).show();
        }
        else {
            if (id.equals("addArrow") && listInFocus.equals("outOfMenuList")) {
                if (insideMenuArray.size() <= 24 - count) {
                    for(int i=0, j=0; i<outOfMenuArray.size(); i++) {
                        if(!toMoveToMenuList[i].equals("empty")) {
                            insideMenuArray.add(toMoveToMenuList[i]);
                            outOfMenuArray.remove(i - j);
                            j++;
                        }
                    }
                }
                else Toast.makeText(getApplicationContext(), "List already contains 24 items.\nYou will need to remove 1 or more items\nbefore adding any", Toast.LENGTH_LONG).show();
            }
            else if (id.equals("removeArrow") && listInFocus.equals("insideMenuList")) {
                for(int i=0, j=0; i<24; i++) {
                    if(!toMoveOutOfMenuList[i].equals("empty")) {
                        outOfMenuArray.add(toMoveOutOfMenuList[i]);
                        insideMenuArray.remove(i - j);
                        j++;
                    }
                }
            }
            else Toast.makeText(getApplicationContext(), "An item cannot be moved to its own list.", Toast.LENGTH_LONG).show();

            populateListViews();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildListArrays();
    }
}

现在我将研究如何将它集成到我正在构建的应用程序中。