我有一个自定义列表视图,显示图像和一些文本,以及3个按钮,只有在项目被点击时才会出现。它们出现在一个简短的下拉动画之后出现。
单击列表中的项目后,按钮会出现,但是在单击相同的项目时,按钮不会从视图中缩回,它们保持活动状态并且可以单击,但是当我单击其他项目时,行会展开并显示按钮,但再次点击后它们也不会崩溃。
另外,我还想知道如果列表上的其他项目展开后如何自动折叠其他项目
这是listView的一行
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="@drawable/placeholder" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:maxLines="3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:id="@+id/listview_toolbar"
android:layout_marginBottom="-50dip"
android:visibility="gone"
android:weightSum="90">
<Button
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/view"
android:focusableInTouchMode="false"
android:id="@+id/view"
android:layout_weight="30" />
<Button
android:layout_width="fill_parent"
android:layout_height="50dip"
android:text="@string/edit"
android:id="@+id/edit"
android:focusableInTouchMode="false"
android:layout_weight="30" />
<Button
android:layout_width="fill_parent"
android:layout_height="50dip"
android:focusableInTouchMode="false"
android:text="@string/remove"
android:id="@+id/remove"
android:layout_weight="30" />
</LinearLayout>
</LinearLayout>
这是我的自定义适配器
public class MyAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
class ViewHolder {
public TextView text;
public ImageView image;
public View toolbar;
}
public MyAdapter(Activity context, ArrayList<String> names) {
super(context, R.layout.ui_row, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// reuse views
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.ui_row, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.image = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.toolbar = convertView.findViewById(R.id.listview_toolbar);
convertView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.text.setText(filelist.get(position));//sets textview of item row in listview
PicassoTools.clearCache(Picasso.with(context));//resets picasso, erases caches
Picasso.with(context).load(new File( Environment.getExternalStorageDirectory() + "//Thumbnails//" + position)).into(holder.image);
((LinearLayout.LayoutParams) holder.toolbar.getLayoutParams()).bottomMargin = -50;
holder.toolbar.setVisibility(View.GONE);
return convertView;
}
}
这是onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
ac = this;
adapter = new MyAdapter(ac, filelist);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
ListViewSetClickListener();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
Toast.makeText(getApplicationContext(), "Button Tap", Toast.LENGTH_SHORT).show();
View toolbar = view.findViewById(R.id.listview_toolbar);
Expand_Custom_Animation expandAni = new Expand_Custom_Animation(toolbar, 500);
toolbar.startAnimation(expandAni);
Button viewB = (Button) view.findViewById(R.id.view);
viewB.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "View", Toast.LENGTH_SHORT).show();
}
});
Button edit = (Button) view.findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Edit", Toast.LENGTH_SHORT).show();
}
});
Button remove = (Button) view.findViewById(R.id.remove);
remove.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Remove", Toast.LENGTH_SHORT).show();
}
});
}
});
}
这是动画类,Expand_Custom_Animation.java
public class Expand_Custom_Animation extends Animation
{
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;
public Expand_Custom_Animation(View view, int duration)
{
setDuration(duration);
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
mMarginStart = mViewLayoutParams.bottomMargin;
mMarginEnd = (mMarginStart == 0 ? (0 - view.getHeight()) : 0);
view.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f)
{
mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
mAnimatedView.requestLayout();
}
else if (!mWasEndedAlready)
{
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter)
{
mAnimatedView.setVisibility(View.GONE);
}
mWasEndedAlready = true;
}
}
}
答案 0 :(得分:0)
原来
android:focusableInTouchMode="false"
我的xml布局中的无效。所以我通过ListView OnclickListener以编程方式为每个按钮创建了这个代码
button.setFocusable(false);
现在正常工作