如何在自定义微调器布局中包含微调器?

时间:2016-09-08 11:20:45

标签: android file android-spinner hierarchy

我正在尝试使用微调器内的微调器来实现文件层次结构。所以我想在微调器的自定义布局中使用Spinner。我无法将微调器上下文赋予微调器,我想知道在Android中实现文件层次结构的任何其他设计?

public class Main2Activity extends AppCompatActivity {
String[] spinnerValues = { "Blur", "NFS", "Burnout","GTA IV", "Racing"};
private WeakReference<Main2Activity> main2ActivityWeakRef;
TextView text;
Spinner mysubSpinner;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    //View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_main2, null);
    //this.setContentView(viewToLoad);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
    ArrayList<String> level1 = new ArrayList<String>();
    level1.add("one");
    level1.add("two");
    level1.add("three");
    level1.add("four");
    myAdapter = new MyAdapter(this, R.layout.custom_spinner, level1);
    myAdapter.setDropDownViewResource(R.layout.custom_spinner);
    mySpinner.setAdapter(myAdapter);
    //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.string_array_name,R.layout.custom_spinner);
    //mySpinner.setDropDownViewResource();
    //mySpinner.setAdapter(adapter);

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d("Main2Activity", "onItemSelected");
            if (!(((Activity) parent.getContext()).isFinishing())) {
                mysubSpinner.setAdapter(new MySubAdapter(parent.getContext(), R.layout.custom_subspinner, spinnerValues));
                // mysubSpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom_spinner, new String[]{spinnerValues[position]}));

            } else {
                Log.d("Main2Activity", "unable ta set adapter");
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

public class MyAdapter extends ArrayAdapter<ArrayList>
{
    Context ctx;
    ArrayList<String> objects;
    public MyAdapter(Context ctx, int txtViewResourceId, ArrayList objects)
    {
        super(ctx, txtViewResourceId, objects);
        this.ctx=ctx;
        this.objects = objects;
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
    {
        return getCustomView(position, cnvtView, prnt);
    }

    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt)
    {
        return getCustomView(pos, cnvtView, prnt);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        //View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
        View mySpinner = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_spinner, null);
        text = (TextView) mySpinner.findViewById(R.id.text);
        text.setText(objects.get(position));
        mysubSpinner = (Spinner) mySpinner.findViewById(R.id.spinner_sub);
        return mySpinner;
    }
}

public class MySubAdapter extends ArrayAdapter<String>
{
    Context ctx;
    public MySubAdapter(Context ctx, int txtViewResourceId, String[] objects)
    { super(ctx, txtViewResourceId, objects);
        this.ctx=ctx;
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
    {
        return getCustomView(position, cnvtView, prnt);
    }

    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt)
    {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        //View mySpinner = inflater.inflate(R.layout.custom_subspinner, parent, false);
        View mySpinner = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_subspinner, null);
        //Spinner mysubSpinner = (Spinner) mySpinner.findViewById(R.id.spinner_sub);
        // mysubSpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom_spinner, new String[]{spinnerValues[position]}));
        //mysubSpinner.setAdapter(new MyAdapter(ctx,R.layout.custom_subspinner,new String[]{spinnerValues[position]}));
        TextView main_text = (TextView) mySpinner
                .findViewById(R.id.text_main_seen);
        main_text.setText(spinnerValues[position]);
        return mySpinner;
    }
}

1 个答案:

答案 0 :(得分:0)

我可以使用此代码解决此问题...

myspiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d("Main2Activity", "onItemSelected");
            myspiner2 = (Spinner) findViewById(R.id.myspiner2);
            if (!(((Activity) parent.getContext()).isFinishing())) {
                if (myspiner.getSelectedItem().equals("two")) {
                    myspiner2.setAdapter(new MySubAdapter(Main3Activity.this, R.layout.custom_spinner, spinnerValues));
                }
                // mysubSpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom_spinner, new String[]{spinnerValues[position]}));

            } else {
                Log.d("Main2Activity", "unable ta set adapter");
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Screenshot