如何在列表中将适配器设置为自定义微调器?

时间:2018-03-27 07:12:03

标签: android listview spinner android-adapter android-listfragment

我有一个ListFragment列表,其中包含复杂的布局作为项目,这些项目包含自定义微调器MaterialSpinner

    MaterialSpinner chooseSubjectSpinner = adapter.getItem(position).findViewById(R.id.myCustomSpinner);
    chooseSubjectSpinner.setAdapter(spinnerAdapter);

我尝试getChildAt(position)adapter.getItem(postition)adapter.getView(position, null, parent)list.get(position)添加前缀.findViewById(R.id.myview),但它不起作用。 (它要么在运行时返回NullPointerException,要么根本不做任何事情!)。

修改

如果你们想要整个片段代码:

private ArrayList<View> listViews = new ArrayList<>();
private ViewAdapter adapter;

private List<String> spinnerItems = new ArrayList<>();
private MaterialSpinnerAdapter<String> spinnerAdapter;

public FragmentUnitTest() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_unit_test, container, false);
}

@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState){
    spinnerItems.add("Hey!");
    spinnerItems.add("Hello!");
    spinnerItems.add("Hi!");
    spinnerAdapter = new MaterialSpinnerAdapter<>(getContext(), spinnerItems);
    adapter = new ViewAdapter(listViews, view.getContext());
    setListAdapter(adapter);
    addView(R.layout.cardview_with_spinner);
    MaterialSpinner spinner = adapter.getItem(0).findViewById(R.id.custom_spinner);
    RLogger.e("FragmentUnitTest", spinner);
    spinner.setAdapter(spinnerAdapter);
    getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getActivity(), "Clicked: " + position + " .", Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

像这样创建一个自定义微调器:

公共类PTCustomSpinner扩展了ArrayAdapter {

public PTCustomSpinner(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub

}

@Override
public int getCount() {

    // TODO Auto-generated method stub
    int count = super.getCount();

    return count>0 ? count-1 : count ;


}

如下所示处理//以下XML布局仅包含ID&#34; temp&#34;

的TEXTVIEW
 PTCustomSpinner spinnerArrayAdapter = new PTCustomSpinner(PTExceptionMainPage.this, R.layout.xml_with_only_textview);

    spinnerArrayAdapter.addAll(team_array);
    spinnerArrayAdapter.add("Pick Team");
    spinnerTeam.setAdapter(spinnerArrayAdapter);
    spinnerTeam.setSelection(spinnerArrayAdapter.getCount());
    spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if (spinnerTeam.getSelectedItem() != "Pick Team") {
                teamIdSelected = apiResponseTeam.get(i).getTeamID();
            }
            else if( spinnerTeam.getSelectedItem().equals("Pick Team"))
            {
                View v = spinnerTeam.getSelectedView();
                ((TextView) v).setTextColor(getResources().getColor(R.color.ash));
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });