我是SwipeListView库的新手,轻扫了列表项之一后,此功能的工作方式如下:
该应用程序正在运行,但是视图被弄乱了,使得隐藏的内容(即在刷列表项之后看到的内容)被覆盖在每个列表项的顶部。在下面的图片之后,我粘贴了片段代码。
我怎样才能解决这个问题?
没有SwipeListView:
使用SwipeListView:
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.test.ItemAdapter;
import com.example.test.ItemRow;
import com.example.test.R;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.SwipeListView;
public class MyList extends Fragment {
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
index = list.getFirstVisiblePosition();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
list.setSelectionFromTop(index, 0);
}
ListView list;
int index = 0;
MediaPlayer mp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
{
mp=MediaPlayer.create(getActivity(), R.raw.v1);
return inflater.inflate(R.layout.listview, container, false);
}
@Override
public void onStart() {
super.onStart();
list=(ListView)getView().findViewById(R.id.listView1);
list.setAdapter(new MyAdapter(getActivity()));
}
}
class MyAdapter extends BaseAdapter
{
ArrayList<SingleRow> list;
Context context;
ItemAdapter adapter;
List<ItemRow> itemData;
public MyAdapter(Context c) {
context=c;
list=new ArrayList<SingleRow>();
//Initialize them...
int[] images={
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
for(int i=0;i<6;i++)
{
list.add(new SingleRow(images[i]));
}
}
public int convertDpToPixel(float dp) {
DisplayMetrics metrics =context.getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
MyViewHolder holder=null;
itemData=new ArrayList<ItemRow>();
adapter=new ItemAdapter(context,R.layout.custom_row,itemData);
final SwipeListView swipelistview;
if(row==null)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.list_rows, parent, false);
row.setBackgroundResource(R.drawable.coloria);
holder=new MyViewHolder(row);
SingleRow temp=list.get(position);
holder.imageView.setImageResource(temp.image);
swipelistview = (SwipeListView)row.findViewById(R.id.example_swipe_lv_list);
row.setTag(holder);
}
else
{
swipelistview = (SwipeListView)row.findViewById(R.id.example_swipe_lv_list);
holder=(MyViewHolder)row.getTag();
}
swipelistview.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
}
@Override
public void onClosed(int position, boolean fromRight) {
}
@Override
public void onListChanged() {
}
@Override
public void onMove(int position, float x) {
}
@Override
public void onStartOpen(int position, int action, boolean right) {
Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
}
@Override
public void onStartClose(int position, boolean right) {
Log.d("swipe", String.format("onStartClose %d", position));
}
@Override
public void onClickFrontView(int position) {
Log.d("swipe", String.format("onClickFrontView %d", position));
swipelistview.openAnimate(position); //when you touch front view it will open
}
public void onClickBackView(int position) {
Log.d("swipe", String.format("onClickBackView %d", position));
swipelistview.closeAnimate(position);//when you touch back view it will close
}
public void onDismiss(int[] reverseSortedPositions) {
}
});
swipelistview.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
swipelistview.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);
swipelistview.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
swipelistview.setOffsetLeft(convertDpToPixel(260f));
swipelistview.setOffsetRight(convertDpToPixel(0f));
swipelistview.setAnimationTime(50);
swipelistview.setSwipeOpenOnLongPress(true);
swipelistview.setAdapter(adapter);
for(int i=0;i<10;i++)
{
itemData.add(new ItemRow("item"+i,context.getResources().getDrawable(R.drawable.ic_launcher) ));
}
Typeface font = Typeface.createFromAsset(holder.textView.getContext().getAssets(), "fonts/OpenSans.ttf");
holder.textView.setTypeface(font);
adapter.notifyDataSetChanged();
return row;
}
class MyViewHolder
{
ImageView imageView;
TextView textView;
MyViewHolder(View v)
{
textView=(TextView)v.findViewById(R.id.textView1);
imageView=(ImageView)v.findViewById(R.id.imageView1);
}
}
class SingleRow
{
int image;
SingleRow(int image)
{
this.image=image;
}
}
}
最佳答案
我现在有同样的问题。可能是因为正面布局的背景是透明的。该库不是隐藏代码,而是具有可见的背景。我通过将我的“后”视图隐藏在getView中,然后在滑动时使其可见来克服了这一问题。
mTourneyList.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onOpened(int position, boolean toRight) {
LinearLayout li = (LinearLayout) mTourneyList.getChildAt(position - mTourneyList.getFirstVisiblePosition()).findViewById(R.id.back);
li.setVisibility(View.VISIBLE);
}
或者您可以为前视图设置一些背景。
编辑:似乎“位置”参数未计算屏幕上的行,因此我稍稍更改了代码,并从“位置”中减去了第一个可见位置。