自定义列表视图中的SmoothScrollToPosition时无操作

时间:2018-03-06 12:15:19

标签: java android listview

您好我使用下面的代码,当用户触摸textview时,列表将被更新,触摸列表行将在用户面前。

public class AppAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<AppList> listStorage;
    private Context context;
    SharedPreferences.Editor editor;
    SharedPreferences sharedPreferences;
    int pos = 0;

    public AppAdapter(Context context, List<AppList> customizedListView) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listStorage = customizedListView;
        this.context = context;
    }

    @Override
    public int getCount() {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        pos = sharedPreferences.getInt("pos", 0);
        Log.e("POS",pos+"At Get cpiunt");
        return listStorage.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        editor = PreferenceManager.getDefaultSharedPreferences(context).edit();


        ViewHolder listViewHolder;
        if (convertView == null) {
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);

            listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name);
            listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon);
            convertView.setTag(listViewHolder);
        } else {
            listViewHolder = (ViewHolder) convertView.getTag();
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());


        listViewHolder.textInListView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                editor.putInt("pos", position).apply();
                Log.e("Pressed", position + "");
                MainActivity.installedAppAdapter.notifyDataSetChanged();
                Log.e("Saved", sharedPreferences.getInt("pos", 0) + "value");
            }
        });


        if (pos != 0) {

           MainActivity.userInstalledApps.setSelection(pos);
           editor.putInt("pos",0).apply();
        }

        return convertView;
    }

    static class ViewHolder {

        TextView textInListView;
        ImageView imageInListView;
    }



}

我阅读了很多问题,但没有清楚了解如何使用SmoothScrolltopostion。虽然我按照信息获得了,但我捕获了点击位置并在列表加载期间检索到将列表的保存位置设置给用户。

如果我使用smoothscrolltoposition,则没有动作,但如果我使用上面代码中所示的setselection,我可以在重新加载后在视图顶部看到被点击的项目。

为什么smoothscroll不起作用,我正在对所需操作进行正确编码。请指导。

0 个答案:

没有答案