我正在尝试在事件滑动发生时检索listview的Child Row。我创建了一个BaseAdapter,它设置了一个customView以下是BaseAdapter的GetView方法:
public View getView(int position, View convertView, ViewGroup parent) {
ModelView modelView = null;
ModelClass model=modelList.get(position);
if(convertView==null)
{
modelView=new ModelView(context,model);
}
else{
Log.d("convertView","NotNUll");
modelView=(modelView) convertView;
}
modelView.setModel(diet);
return modelView;
}
我的ModelView类看起来像这样:
public class ModelView extends LinearLayout {
Context context;
TextView text1,text2,text3;
ImageView img1;
Model d;
public ModelView(Context context,Model d) {
super(context);
this.context=context;
this.d=d;
this.setTag(d);
HookUp();
}
public void HookUp() {
this.setLayoutParams(new ListView.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
this.setOrientation(LinearLayout.VERTICAL);
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.lay, null);
this.addView(view);
textDietHeadLine=(TextView) view.findViewById(R);
text=(TextView) view.findViewById(R.id.txt1);
text=(TextView) view.findViewById(R.id.txt2);
}
}
在TouchListener上的我的主要活动中,当我检索标签时,它总是给我null,我不知道我哪里出错...任何帮助都会受到赞赏,虽然我在这个问题上搜索了很多但是没有用,我是android的初学者,现在安静了一段时间,plss helppp :(
以下是touchListener代码:
public boolean onTouch(View v, MotionEvent e)
{
if (gestureDetector.onTouchEvent(e)){
// Log.d("Diet d","v.gettag());
return true;
}else{
return false;
}
答案 0 :(得分:0)
它有点复杂,我希望我能做得对,如果你想获得用户刷上的特定行,它需要提取一个深入ListView调用findMotionRow的方法,这个方法声明作为扩展程序的实际私有可见性,因此我们必须在我们自己的ListView中使用该方法,并学习如何从ListView的源代码中调用它:
public class YourListView extends ListView {
...Constructors...
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mMotionPosition = findMotionRow(ev.getY());
break;
}
return super.onInterceptTouchEvent(ev);
}
private int mMotionPosition;
public int getMotionPosition() {
return mMotionPosition;
}
/**
* Find which position is motion on.
* Note : this method copy into public from 4.0 source code.
* @param y Y coordinate of the motion event.
* @return Selected index (starting at 0) of the data item.
*/
private int findMotionRow(float y) {
int childCount = getChildCount();
if (childCount > 0) {
if (!isStackFromBottom()) {
for (int i = 0; i < childCount; i++) {
View v = getChildAt(i);
if (y <= v.getBottom()) {
return getFirstVisiblePosition() + i;
}
}
} else {
for (int i = childCount - 1; i >= 0; i--) {
View v = getChildAt(i);
if (y >= v.getTop()) {
return getFirstVisiblePosition() + i;
}
}
}
}
return INVALID_POSITION;
}
}
在适当的地方,即Activity.onCreate(),我们可以听取onTouch事件并能够使用mMotionPosition
来获取行的视图:
mYourListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int hoverChildIndex =
mYourListView.getMotionPosition() - mYourListView.getFirstVisiblePosition();
ModelView hoveringView = (ModelView) mYourListView.getChildAt(hoverChildIndex);
return false;
}
});