微调器不会在片段中显示下拉菜单

Spinner wont show dropdown in fragment

我创建了一个名为 PaletteFragment 的片段,其中包含一个微调器。但是,当我单击微调器时,下拉项不会出现。它允许我点击微调器,但不执行任何操作。当作业不需要我使用片段(如下所示)时,微调器 工作 但现在我们需要使用片段,微调器不工作,即使我是在 logcat 中没有错误。

这就是我试图从片段中再次得到的

这是我将 PaletteActivity 移动到片段后得到的结果

这是我的 PaletteFragment

package edu.temple.coloractivity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import java.util.Locale;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PaletteFragment #newInstance} factory method to
 * create an instance of this fragment.
 */
public class PaletteFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public static PaletteFragment newIntance(){
        return new PaletteFragment();
    }
    public PaletteFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PaletteFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PaletteFragment newInstance(String param1, String param2) {
        PaletteFragment fragment = new PaletteFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_palette, container, false);
        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getActivity(), R.array.myColors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> stringNames = ArrayAdapter.createFromResource(getActivity(), R.array.myStrings, android.R.layout.simple_spinner_item);
        stringNames.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> stringName = ArrayAdapter.createFromResource(getActivity(),R.array.myStrings, android.R.layout.simple_spinner_item);
        stringName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        View v = inflater.inflate(R.layout.fragment_palette,container,false);
        spinner = v.findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0){
                }
                else if(position == 1){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",1);
                    startActivity(i);
                }else if(position == 2){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",2);
                    startActivity(i);
                }else if(position == 3){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",3);
                    startActivity(i);
                }else if(position == 4){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",4);
                    startActivity(i);
                }else if(position == 5){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",5);
                    startActivity(i);
                }else if(position == 6){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",6);
                    startActivity(i);
                }else if(position == 7){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",7);
                    startActivity(i);
                }else if(position == 8){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",8);
                    startActivity(i);
                }else if(position == 9){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",9);
                    startActivity(i);
                }else if(position == 10){
                    Intent i = new Intent(getActivity(), CanvasActivity.class);
                    i.putExtra("position",10);
                    startActivity(i);
                }

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

}

这是我的 PaletteActivity

package edu.temple.coloractivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import java.util.Locale;


public class PaletteActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PaletteFragment PaletteFragment = new PaletteFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.firstLayout,PaletteFragment,PaletteFragment.getTag())
                .commit();
    }
}

这是相关代码,希望它足以发现我做错了什么,否则我会 post 整个代码

你做的一切都很好,除了你没有绑定适配器和微调器。 使用它来设置微调器的适配器。

stringName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            View v = inflater.inflate(R.layout.fragment_palette,container,false);
            spinner = v.findViewById(R.id.spinner);
            spinner.setAdapter(stringName )