我创造了一个微调器。其中,我从JSON文件获取微调项目。
这里是Adapter.java
package adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.angelnx.cricvilla.cricvilla.R;
import java.util.List;
public class ScheduleSpinnerAdapter extends ArrayAdapter<String>{
private Context context;
private List<String> arr_data;
public ScheduleSpinnerAdapter(Context context,int resource,List<String> arr_data){
super(context,resource,arr_data);
this.context=context;
this.arr_data=arr_data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.spinner_row,parent,false);
TextView txtname=(TextView)convertView.findViewById(R.id.spinner_value);
txtname.setText(this.arr_data.get(position));
// convertView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(context, "Selected"+position, Toast.LENGTH_SHORT).show();
// }
// });
return convertView;
}
@Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.spinner_row,parent,false);
TextView txtname=(TextView)convertView.findViewById(R.id.spinner_value);
txtname.setText(this.arr_data.get(position));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Selected" + position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
这是我的spinner_row.xml,它在适配器的两个方法中都会膨胀。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:id="@+id/spinner_value"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="what"
android:padding="10dp"
android:gravity="center|start"
android:textColor="@color/txtColor"/>
</LinearLayout>
这里,将值设置为适配器
ArrayAdapter adapter=new ScheduleSpinnerAdapter(getContext(),android.R.layout.simple_spinner_item,item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
问题是在选择项目表格drowpdown之后。在我的案例中,Drowpdown并不隐藏。
请帮我解决这个问题。
答案 0 :(得分:3)
调用onDetachedFromWindow()
会隐藏Spinner下拉列表。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Spinner spinner = (Spinner) context.findViewById(R.id.mySpinner);
if (spinner != null) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(spinner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});