我检查了所有可能的解决方案,以便在ListView中处理EditText的焦点。 我在片段中有一个ListView。此ListView的适配器由ImageButton,ImageView和EditText组成。我希望EditText可以集中精力。 我尝试了一些解决方案,其中一个是:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity().getApplicationContext(), list);
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
adapter.nome.requestFocus();
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
});
lv.setAdapter(adapter);
return view;
}
}
然后在我的XML文件中,我的ListView中有android:descendantFocusability="beforeDescendants"
。
这是我的FileAdapter类:
public class FileAdapter extends ArrayAdapter<Files> {
LayoutInflater inflater;
EditText nome;
private final Context context;
private ArrayList<Files> list;
public FileAdapter(Context context, ArrayList<Files> list) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.homeadapter, null);
nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
return convertView;
}
}
显然我在这里只放了我的代码的有用部分,正如我之前说的,我的适配器还有其他元素,但对问题没用。 我尝试的解决方案不起作用,所以我正在搜索另一个。
答案 0 :(得分:0)
昨天我的片段有类似的问题...这是我的片段代码:
public static class PlaceholderFragment_MainActivity extends Fragment implements View.OnClickListener {
//Declaration of classes
Floor_Adapter FloorsAdapter;
Floor_List Floors = new Floor_List();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Declaration of controls
View view = inflater.inflate(R.layout.building_view, container, false);
TextView txtvHotelName = (TextView) view.findViewById(R.id.txtvHotelName);
ListView lvFloors = (ListView) view.findViewById(R.id.lvFloors);
txtvHotelName.setText("Hotel");
Floors.setFloorsArray();
FloorsAdapter = new Floor_Adapter(getActivity(),Floors.getFloorsArray(),lvFloors);
lvFloors.setAdapter(FloorsAdapter);
return view;
}
@Override
public void onClick(View view) {
String A;
TextView floor = (TextView)view.findViewById(R.id.txtvFloor);
A = floor.getText().toString();
}
}
这是我的适配器代码:
public class Floor_Adapter extends ArrayAdapter<Floor_Object>{
//Declaration of atributes
private ArrayList<Floor_Object> Floor_List;
private final Activity context;
private final ListView lvFloors;
public Floor_Adapter(Activity context,ArrayList<Floor_Object> Floors_Array,ListView lvFloors) {
super(context,R.layout.floor_layout,Floors_Array);
this.context = context;
this.Floor_List = Floors_Array;
this.lvFloors = lvFloors;
}
public static class Floor_View{
//Declaration of Atributes
TextView FloorName;
Button btnFloorRow;
ImageView CheckImage;
TextView NoChecks;
ImageView BlockImage;
TextView NoBlocks;
ImageView DirtImage;
TextView NoDirts;
ImageView TroubleImage;
TextView NoTroubles;
ImageButton btnShowTasks;
ImageButton btnShowRooms;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent) {
//Declaration of Variables
Floor_View rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new Floor_View();
ConvertView = inflator.inflate(R.layout.floor_layout,null,true);
rowView.FloorName = (TextView) ConvertView.findViewById(R.id.txtvFloor);
rowView.btnFloorRow = (Button) ConvertView.findViewById(R.id.btnFlooRow);
rowView.CheckImage = (ImageView) ConvertView.findViewById(R.id.ivCheck);
rowView.NoChecks = (TextView) ConvertView.findViewById(R.id.txtvCheckRooms);
rowView.BlockImage = (ImageView) ConvertView.findViewById(R.id.ivBlockRooms);
rowView.NoBlocks = (TextView) ConvertView.findViewById(R.id.txtvBlockedRooms);
rowView.DirtImage = (ImageView) ConvertView.findViewById(R.id.ivDirtRooms);
rowView.NoDirts = (TextView) ConvertView.findViewById(R.id.txtvDirtRooms);
rowView.TroubleImage = (ImageView) ConvertView.findViewById(R.id.ivTroubleRooms);
rowView.NoTroubles = (TextView) ConvertView.findViewById(R.id.txtvTroubleRooms);
rowView.btnShowTasks = (ImageButton) ConvertView.findViewById(R.id.btnShowTasks);
rowView.btnShowRooms = (ImageButton) ConvertView.findViewById(R.id.btnShowRooms);
ConvertView.setTag(rowView);
}
else
{
rowView = (Floor_View) ConvertView.getTag();
}
Floor_Object Floor = Floor_List.get(position);
rowView.FloorName.setText(Floor.getFloorName());
if(Floor.getNoChecks().equals("0")) {
rowView.CheckImage.setVisibility(View.INVISIBLE);
rowView.NoChecks.setVisibility(View.INVISIBLE);
}
else{
rowView.CheckImage.setVisibility(View.VISIBLE);
rowView.NoChecks.setVisibility(View.VISIBLE);
rowView.NoChecks.setText(Floor.getNoChecks());
}
if(Floor.getNoBlocks().equals("0")) {
rowView.BlockImage.setVisibility(View.INVISIBLE);
rowView.NoBlocks.setVisibility(View.INVISIBLE);
}
else{
rowView.BlockImage.setVisibility(View.VISIBLE);
rowView.NoBlocks.setVisibility(View.VISIBLE);
rowView.NoBlocks.setText(Floor.getNoBlocks());
}
if(Floor.getNoDirts().equals("0")) {
rowView.DirtImage.setVisibility(View.INVISIBLE);
rowView.NoDirts.setVisibility(View.INVISIBLE);
}
else{
rowView.DirtImage.setVisibility(View.VISIBLE);
rowView.NoDirts.setVisibility(View.VISIBLE);
rowView.NoDirts.setText(Floor.getNoDirts());
}
if(Floor.getNoTroubles().equals("0")) {
rowView.TroubleImage.setVisibility(View.INVISIBLE);
rowView.NoTroubles.setVisibility(View.INVISIBLE);
}
else{
rowView.TroubleImage.setVisibility(View.VISIBLE);
rowView.NoTroubles.setVisibility(View.VISIBLE);
rowView.NoTroubles.setText(Floor.getNoTroubles());
}
if(Floor.getNoBlocks().equals("0") && Floor.getNoDirts().equals("0") && Floor.getNoTroubles().equals("0")) {
rowView.btnFloorRow.setClickable(false);
rowView.btnShowTasks.setClickable(false);
rowView.btnShowTasks.setImageResource(R.drawable.list_999999_50);
}
else{
rowView.btnFloorRow.setClickable(true);
rowView.btnFloorRow.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setClickable(true);
rowView.btnShowTasks.setOnClickListener(OnShowTasksClickListener);
rowView.btnShowTasks.setImageResource(R.drawable.list_3a4b66_50);
}
rowView.btnShowRooms.setOnClickListener(OnShowRoomsClickListener);
return ConvertView;
}
private View.OnClickListener OnShowTasksClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
private View.OnClickListener OnShowRoomsClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = lvFloors.getPositionForView((View) v.getParent());
}
};
}
你可以看到我在处理片段时必须在适配器中创建控制事件...只需更改视图事件并确认它是您的需要,我想......我在等待样品可以帮助你。如果您需要更多信息或帮助建议我,祝您好运!
PD:我认为这是您需要的代码:
public class HomeFragment extends Fragment {
FileAdapter adapter;
ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView)view.findViewById(R.id.listView);
adapter = new FileAdapter(getActivity(), list);
lv.setAdapter(adapter);
return view;
}
}
public class FileAdapter extends ArrayAdapter<Files> {
private final Context context;
private ArrayList<Files> list;
private final ListView Lv;
public FileAdapter(Context context, ArrayList<Files> list,ListView lv) {
super(context, R.layout.homeadapter, list);
this.context = context;
this.list = list;
this.Lv = lv;
}
public static class RowView{
//Declaration of Atributes
EditText Nome;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
RowView rowView;
LayoutInflater inflator = context.getLayoutInflater();
if(ConvertView == null){
rowView = new RowView();
convertView = inflator.inflate(R.layout.homeadapter, null);
rowView.nome = (EditText) convertView.findViewById(R.id.tvNomeFile);
}
rowView.nome.setOnItemSelectedListener(ItemSelecteListener);
return convertView;
}
private View.setOnItemSelectedListener ItemSelecteListener = new View.OnClickListener() {
@Override
public void onItemSelected(View view) {
final int position = lvFloors.getPositionForView((View) v.getParent());
if (position == 1){
lv.setItemsCanFocus(true);
lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}else{
if (!lv.isFocused()){
lv.setItemsCanFocus(false);
lv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
lv.requestFocus();
}
}
}
};
}