如何在滚动列表视图时禁用列表项单击

时间:2017-01-07 08:25:19

标签: android listview adapter

我创建了一个列表视图,在其中加载来自sq-lite的数据,这是使用自定义适配器完成的。在滚动列表视图时,列表项被选中,但我想禁用列表项当滚动并在滚动结束时启用,任何人都可以告诉我该怎么做。

 listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                int btn_initPosY = fabaddnew.getScrollY();
                int li_initPosY = li_general.getScrollY();
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    fabaddnew.animate().cancel();
                    li_general.animate().cancel();
                    fabaddnew.animate().translationYBy(150);
                    li_general.animate().translationYBy(150);
                } else {
                    fabaddnew.animate().cancel();
                    li_general.animate().cancel();
                    fabaddnew.animate().translationY(btn_initPosY);
                    li_general.animate().translationY(li_initPosY);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                //what is the bottom item that is visible
                int lastInScreen = firstVisibleItem + visibleItemCount;

                //is the bottom item visible & not loading more already? Load more!
                if ((lastInScreen == totalItemCount) && !(loadingMore)) {

                    new LoadDataTask().execute();
                }
            }
        });

适配器:

 public class Daybook_adapter extends BaseAdapter{
 Context context;
Activity activity;
ArrayList<Daybook> entriesdaybook;
ArrayList<Daybooklist> daybooklists;
Daybooklist_adapter adapter;
DatabaseHandler databaseHandler;
LinearLayout emptyy;
double totalamountin = 0.0;
ExpandableHeightListView daybookdetailviewlist;



//DaybookSwipeMenuListView daybookdetailviewlist;
public Daybook_adapter(Activity activity, ArrayList<Daybook> list) {

    //this.context = context;
    this.activity = activity;
    entriesdaybook = list;

}


@Override
public int getCount() {
    return entriesdaybook.size();
}

@Override
public Object getItem(int position) {

    return entriesdaybook.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}



@Override
public View getView(int position, View convertView, ViewGroup arg2) {


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.model_daybook, null);

    }


    final Daybook m = entriesdaybook.get(position);
    final TextView tv_date = (TextView) convertView.findViewById(R.id.tv_daybook_date);
    final TextView tv_cashin = (TextView) convertView.findViewById(R.id.tv_daybook_cashin);
    final TextView tv_cashout = (TextView) convertView.findViewById(R.id.tv_daybook_cashout);
    final TextView tv_totalamt = (TextView) convertView.findViewById(R.id.daybook_total_amt);
    final ImageView img_pdf = (ImageView) convertView.findViewById(R.id.img_printpdf);
    //final String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    String s = m.getDate();
    emptyy = (LinearLayout) convertView.findViewById(R.id.empty);
    // daybookdetailviewlist = (DaybookSwipeMenuListView) convertView.findViewById(R.id.detaillist_daybook);
    daybookdetailviewlist = (ExpandableHeightListView) convertView.findViewById(R.id.detaillist_daybook);

    String[] spiliter = s.split("-");
    String year = spiliter[0];
    String month = spiliter[1];
    String date = spiliter[2];
    if (month.startsWith("01")) {
        tv_date.setText(date + "Jan" + year);
    } else if (month.startsWith("02")) {
        tv_date.setText(date + "Feb" + year);
    } else if (month.startsWith("03")) {
        tv_date.setText(date + "Mar" + year);
    } else if (month.startsWith("04")) {
        tv_date.setText(date + "Apr" + year);
    } else if (month.startsWith("05")) {
        tv_date.setText(date + "May" + year);
    } else if (month.startsWith("06")) {
        tv_date.setText(date + "Jun" + year);
    } else if (month.startsWith("07")) {
        tv_date.setText(date + "Jul" + year);
    } else if (month.startsWith("08")) {
        tv_date.setText(date + "Aug" + year);
    } else if (month.startsWith("09")) {
        tv_date.setText(date + "Sep" + year);
    } else if (month.startsWith("10")) {
        tv_date.setText(date + "Oct" + year);
    } else if (month.startsWith("11")) {
        tv_date.setText(date + "Nov" + year);
    } else if (month.startsWith("12")) {
        tv_date.setText(date + "Dec" + year);
    }


   /* if (m.getUsertype().startsWith("singleworker")) {
        tv_cashin.setText("\u20B9" + "0");
    } else if (m.getUsertype().startsWith("groupworker")) {
        tv_cashin.setText("\u20B9" + "0");
    }*/

    tv_cashin.setText("\u20B9" + m.getCashin());
    tv_cashout.setText("\u20B9" + m.getCashout());
    double one = Double.parseDouble(m.getCashin());
    double two = Double.parseDouble(m.getCashout());
    double three = one + two;
    tv_totalamt.setText("\u20B9" + String.valueOf(three));
    databaseHandler = new DatabaseHandler(activity);
    daybooklists = databaseHandler.getAllDaywisedaybookdetails(s);
    adapter = new Daybooklist_adapter(activity, daybooklists);
    if (adapter != null) {
        if (adapter.getCount() > 0) {
            emptyy.setVisibility(View.GONE);
            daybookdetailviewlist.setAdapter(adapter);

        }
    } else {
        daybookdetailviewlist.setEmptyView(emptyy);
    }
    daybookdetailviewlist.setExpanded(true);
    daybookdetailviewlist.setFastScrollEnabled(true);

    img_pdf.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    activity);

            // set title
            alertDialogBuilder.setTitle(R.string.app_name);

            // set dialog message
            alertDialogBuilder
                    .setMessage("Click yes to Print Report for : "+ m.getDate())
                    .setCancelable(true)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity
                            Intent pdfreport = new Intent(activity,Activity_Daybookpdf.class);
                            pdfreport.putExtra("date",m.getDate());
                            activity.startActivity(pdfreport);
                        }
                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
            Button nbutton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
            nbutton.setTextColor(activity.getResources().getColor(R.color.colorAccent));
            Button pbutton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            pbutton.setBackgroundColor(activity.getResources().getColor(R.color.colorAccent));
            pbutton.setPadding(0, 10, 10, 0);
            pbutton.setTextColor(Color.WHITE);
            return false;
        }
    });


    return convertView;

}

public void setTransactionList(ArrayList<Daybook> newList) {
    entriesdaybook = newList;
    notifyDataSetChanged();
}

}

2 个答案:

答案 0 :(得分:1)

实现滚动检测的侦听器:

public interface JScrollListener {
    void onScrollStarted();
    void onScrollStopped();
}

以这种方式扩展ListView:

public class JListView extends ListView {
    private static final long OFFSET_DELAY = 150;
    private boolean isScrolling;
    private JScrollListener jScrollListener;

    public JListView(Context context) {
        super(context);
    }

    public JListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public JListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public JListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setJScrollListener(JScrollListener jScrollListener) {
        this.jScrollListener = jScrollListener;
    }

    Handler handler;
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(!isScrolling) {
            isScrolling = true;
            if(jScrollListener!=null)
                jScrollListener.onScrollStarted();
        }
        if(handler!=null)
            handler.removeCallbacksAndMessages(null);
        else
            handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(isScrolling) {
                    isScrolling = false;
                    if(jScrollListener!=null)
                        jScrollListener.onScrollStopped();
                }
            }
        }, OFFSET_DELAY);
    }
}

使用它像:

JListView jListView = (JListView) findViewById(R.id.jListView);
jListView.setJScrollListener(new JScrollListener() {
            @Override
            public void onScrollStarted() {
                clickEnabled(false);
            }

            @Override
            public void onScrollStopped() {
                clickEnabled(true);
            }
        });
希望能帮到你!

答案 1 :(得分:0)

试试这个:

listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

            if (scrollState == SCROLL_STATE_IDLE) {
                Log.i("a", "scrolling stopped...");
                isScrolling = false;
            }

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
           isScrolling = true;
        }
    });

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            if(!isScrolling){
                //do your task
            }

        }
    });