我创建了一个ListView,每行都有一个EditText。 ListView还允许用户使用https://github.com/romannurik/Android-SwipeToDismiss中的SwipeDismissListViewTouchListener.java水平滑动来删除列表中的项目。
两种功能都能很好地工作,除非用户尝试使用EditText上发生的初始ACTION_DOWN事件进行滑动手势。看起来EditText正在使用ACTION_DOWN事件,然后才能将其传递给SwipeDismissListViewTouchListener,后者需要ACTION_DOWN事件来初始化手势。
问题:
如何在EditText上启动滑动手势时让SwipeDismissListViewTouchListener工作?
SampleListFragment.java:
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class SampleListFragment extends ListFragment {
protected AddGroupAdapter adapter;
protected ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new AddGroupAdapter(getActivity());
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
lv = getListView();
SwipeDismissListViewTouchListener listener =
new SwipeDismissListViewTouchListener(
lv,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.removeRecord(position);
}
adapter.notifyDataSetChanged();
}
});
lv.setOnTouchListener(listener);
lv.setOnScrollListener(listener.makeScrollListener());
super.onActivityCreated(savedInstanceState);
}
public class AddGroupAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> data;
public AddGroupAdapter(Activity activity) {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("Sample Text " + String.valueOf(i));
}
notifyDataSetChanged();
}
public void removeRecord(int position) {
data.remove(position);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, null);
holder.edit = (EditText) convertView.findViewById(R.id.edit_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.edit.setText(data.get(position));
holder.edit.setId(position);
holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
final int position = view.getId();
final EditText edit = (EditText) view;
if (edit.getText() != null && !edit.getText().toString().isEmpty())
if (position < getCount())
data.set(position, edit.getText().toString());
}
}
});
return convertView;
}
}
static class ViewHolder {
EditText edit;
}
}
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="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/edit_text"
android:hint="Enter Text"
android:gravity="center_horizontal"
android:singleLine="true" />
</LinearLayout>
* fragment_sample.xml:*
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/list"
android:name="com.example.app.SampleListFragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:descendantFocusability="beforeDescendants"
tools:context=".SampleActivity"
tools:layout="@android:layout/list_content"/>
</LinearLayout>
答案 0 :(得分:0)
尝试将其添加到row.xml中的LinearLayout
:
android:descendantFocusability="blocksDescendants"
编辑:
显然,EditText
比列表视图中的按钮更棘手。也许这个答案可以让你朝着正确的方向前进,如果你必须把它们放在你的身上:https://stackoverflow.com/a/2680077/362298