自动滚动到适配器中的位置

时间:2015-09-17 22:22:34

标签: java android listview baseadapter

我试图让程序自动滚动到全局整数AtlasApplication.sSelectedPositionForLaw设置的整数位置。我尝试过使用mNotificationLv.smoothScrollToPostion(AtlasApplication.sSelectedPositionForLaw)而没有运气在SubMenuActivity中。如何让SubMenuActivity自动滚动到适配器中的选定位置?

SubMenuActivity

@Override
protected void onResume() {
    try {
        super.onResume();
        FontLoader.loadFonts(this);
        headerLayout.LoadHeaderFont();
        footerLayout.LoadFooterFont();
        FontLoader.setRalewayBoldFont(mSubMenuHeaderTv);
        FontLoader.setAwesomeFont(mSubMenuBackTv);
        FontLoader.setAtlasFont(mSubMenuIconTv);
        loadSubMenuLabel();
        subMenuAdapter.setList(AtlasApplication.lstLawsForLocation);
        mNotificationLv.setAdapter(subMenuAdapter);
        mNotificationLv.smoothScrollToPosition(21);

        activityResumed();
    }
    catch(Exception e){
        exceptionHandler.handle(e, "onResume()");
    }
} 

SubMenuAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_sub_menu_item, null);
        holder = new ViewHolder();
        holder.rowSubMenuLl = (LinearLayout) convertView.findViewById(R.id.row_sub_menu_ll);
        holder.mSubMenuTitleTv = (TextView) convertView.findViewById(R.id.sub_menu_title_tv);
        holder.mSubMenuDetailPane = (LinearLayout) convertView.findViewById(R.id.sub_menu_detail_pane_ll);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    FontLoader.setRalewayRegularFont(holder.mSubMenuTitleTv);

    LawModel notificationModel = lstSubMenuModels.get(position);

    if(Integer.parseInt(notificationModel.getTagID()) == AtlasApplication.sSelectedPositionForLaw) {
        notificationModel.setSelected(true);
    }

    holder.mSubMenuTitleTv.setText(notificationModel.getTagName().trim());

    if (notificationModel.isSelected) {
        //AtlasApplication.SubMenuTitle = notificationModel.getTagName();
        AtlasApplication.sLawModelSelected = notificationModel;
        AtlasApplication.sSelectedPositionForLaw=position;
        if (holder.mSubMenuDetailPane.getChildCount() < 1) {
            SpeedingFines sf = new SpeedingFines(context, holder.mSubMenuDetailPane);
            sf.setPriorityListener(mOnPriorityUpdateListener);

            holder.rowSubMenuLl.setBackgroundResource(R.color.taxes_bg);
            holder.mSubMenuDetailPane.requestFocus();
        }
    } else {
        holder.mSubMenuDetailPane.removeAllViews();
        holder.rowSubMenuLl.setBackgroundResource(R.color.grad_dark);
    }

    holder.mSubMenuTitleTv.setId(position);
    holder.mSubMenuTitleTv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            setAllselectedFalse(v.getId());
            lstSubMenuModels.get(v.getId()).isSelected = !lstSubMenuModels.get(v.getId()).isSelected;
            notifyDataSetChanged();
        }
    });
    return convertView;
}

private void setAllselectedFalse(int selectedRow) {
    AtlasApplication.sSelectedPositionForLaw=-1;
    for (int i = 0; i < lstSubMenuModels.size(); i++) {
        if (i != selectedRow) {
            lstSubMenuModels.get(i).setSelected(false);
        }
    }
}

public void setList(List<LawModel> subMenuModels) {
    this.lstSubMenuModels = subMenuModels;
    notifyDataSetChanged();
}

private class ViewHolder {
    TextView mSubMenuTitleTv;
    LinearLayout rowSubMenuLl, mSubMenuDetailPane;
}

2 个答案:

答案 0 :(得分:1)

以前我正在做类似的事情,我可以用这种方式滚动到自定义位置:

int sum = 0;
for (int i = 0; i < mListView.getChildCount(); i++) {
    if(sum > position){
        mListView.smoothScrollToPosition(i);
        break;
    }
    sum = sum + mListView.getChildAt(i).getHeight();
}

所以,在你的情况下,它可以是:

    listView.post(new Runnable() {
        @Override
        public void run() {
            listView.smoothScrollToPosition(position);
        }
    });

}

我认为的原因是

  基于事物队列的android工作的UI(用户界面)   做,并在一个可运行的post函数中,你告诉UI,   什么时候可以,在完成队列中的所有事情之后执行一些   动作,在这种情况下滚动到listView的某个位置。

答案 1 :(得分:-1)

我来这里是为了解决你的问题:

计算到达正确位置的观看次数,几乎可以到达您的位置:

/* set each relative cell to 'X' */
for (i = 0; i < Sx; i++) {
    displayData[scale_x * trajData[i][0]][scale_y * trajData[i][1]] = 'X';
}

好LUCk ......

相关问题