SwipeListView:视图搞砸了。隐藏的内容遍布列表项

时间:2014-02-17 12:53:45

标签: android android-listview android-library

我是SwipeListView库的新手,当其中一个列表项被刷过时,该功能的工作原理如下:

enter image description here

应用程序正在运行,但是视图混乱,使得隐藏的内容(即,在滑动列表项之后看到的内容)被覆盖在每个列表项的顶部。按照下面的图片,我已经粘贴了片段代码。

我该如何解决这个问题?

没有SwipeListView: enter image description here

使用SwipeListView: enter image description here

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;
        }
    }

    }

2 个答案:

答案 0 :(得分:5)

我现在遇到同样的问题。这可能是因为您的正面布局背景是透明的。该库不会隐藏代码,但具有可见的背景。我通过隐藏我的背部来克服这个问题。在我的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);             
        }

或者您可以在前视图中设置一些背景。

编辑:似乎&#39;位置&#39;参数没有计算屏幕上的行,所以我稍微更改了代码并从&#39;位置&#39;中减去了第一个可见位置。

答案 1 :(得分:1)

我有同样的问题,并通过使用这篇文章绕过它: Setting background colour of Android layout element

“您可以使用简单的颜色资源,通常在 res / values / colors.xml 中指定。

<color name="white">#ffffffff</color>

并在布局xml

中通过android:background="@color/white"使用此功能

确保将其应用于前后布局