我正在使用当前应用中的Fragments
。我尝试在其中一个片段中使用Spinner,以特定格式对其下方的listview
进行排序。 Adapter
设置正确,但我无法打开触摸下拉菜单以选择选项。我怎么知道适配器设置正确,是AVD
,我可以使用键盘导航到它并打开它。
代码:
public class Upcoming_Reminders extends Fragment {
private View view;
private ProgressDialog progressDialog;
private ListView listView;
private String TAG = "Loggin";
RemindersAdapter remindersAdapter;
private Spinner mSortSpinner;
private String[] test;
public Upcoming_Reminders() {
// Required empty public constructor
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSortSpinner = (Spinner) view.findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getContext(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSortSpinner.setAdapter(mSortAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_upcoming__reminders, container, false);
test = getActivity().getResources().getStringArray(R.array.sort_by);
listView = (ListView) view.findViewById(R.id.contracts_list);
return view;
}
我也尝试将其放在OnCreateView
方法中,位于视图分配下方。它仍然无法点击。
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.onerooftechnologiesamc.Upcoming_Reminders">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="70dp">
<TextView
android:id="@+id/te"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|end"
android:text="@string/sort_by" />
<Spinner
android:id="@+id/sort_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
/>
</LinearLayout></FrameLayout>
可能是什么原因?我试了很多谷歌,没有找到与我完全相同问题的人。
谢谢
编辑1:资源文件
<resources>
<array name="sort_by">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</array></resources>
答案 0 :(得分:0)
试试这个:
public class SpinnerAdapter extends ArrayAdapter<String>
{
private Activity context;
String[] data = null;
public SpinnerAdapter(Activity context, int resource,
String[] data2)
{
super(context, resource, data2);
this.context = context;
this.data = data2;
}
...
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent)
{
View row = convertView;
if(row == null)
{
//inflate your customlayout for the textview
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_layout, parent, false);
}
//put the data in it
String item = data[position];
if(item != null)
{
TextView text1 = (TextView) row.findViewById(R.id.rowText);
text1.setTextColor(Color.WHITE);
text1.setText(item);
}
return row;
}
...
}
然后为微调器设置适配器:
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
final String[] data = getResources().getStringArray(
R.array.data);
final ArrayAdapter<String> adapter = new SpinnerAdapter(
MainActivity.this, android.R.layout.simple_spinner_item,
data);
mySpinner.setAdapter(adapter);
答案 1 :(得分:0)
public class SpinnerFragment extends Fragment {
private Spinner spinner;
private String[] test = {"1","2","3","4","5"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner,container,false);
spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mSortAdapter);
return view;
}
}
答案 2 :(得分:0)
问题似乎与FrameLayout有关。我切换到了RelativeLayout,它开始完美运行。