自动滚动RecyclerView到项目的最后位置?

时间:2017-03-04 23:20:15

标签: c# android visual-studio mono xamarin.android

我正在尝试这段代码:

ini_set('allow_url_fopen', 'on');//I set this to ensure,but nothin happens
$xml = simplexml_load_file($file);

XAML布局是:

public class ping : SupportFragment
{
View view;
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
public List<data> mdata;
int i = 0;


public override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    view = inflater.Inflate(Resource.Layout.ping, container, false);
    mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerview_ping);
    mLayoutManager = new LinearLayoutManager(Activity);
    mRecyclerView.SetLayoutManager(mLayoutManager);
    mdata = new List<data>();
    mAdapter = new RecyclerAdapter(mdata,mRecyclerView);
    mRecyclerView.SetAdapter(mAdapter);
    mRecyclerView.SetItemClickListener((rv, position, view) =>
    {
        //An item has been clicked
        Toast.MakeText(mRecyclerView.Context, mdata[position].sekuenca, ToastLength.Long).Show();

    });
    Task.Run(() => add_data());

    return view;
}

public void add_data()
{
    fillimi:
    Activity.RunOnUiThread(() =>
    {
        i = i + 1;
        string sekuena = i.ToString();
        mdata.Add(new data() { titulli = "From : eakteam.com", sekuenca = "Secuenca : " + sekuena, madhesia = "Send : 64 Bytes", koha = "Time : 45 ms" });
        mAdapter.NotifyItemInserted(mdata.Count());
        mRecyclerView.SmoothScrollToPosition(mdata.Count());
    });

    Thread.Sleep(2000);
    goto fillimi;
}
public class data
{
    public string titulli { get; set; }
    public string sekuenca { get; set; }
    public string madhesia { get; set; }
    public string koha { get; set; }
}
public class RecyclerAdapter : RecyclerView.Adapter
{
    private List<data> mdata;
    private RecyclerView mRecyclerView;
    public RecyclerAdapter(List<data> data, RecyclerView recyclerView)
    {
        mdata = data;
        mRecyclerView = recyclerView;
    }
    public class MyView : RecyclerView.ViewHolder
    {
        public View mMainView { get; set; }
        public TextView mtitulli { get; set; }
        public TextView msekuenca { get; set; }
        public TextView mmadhesia { get; set; }
        public TextView mkoha { get; set; }

        public MyView(View view) : base(view)
        {
            mMainView = view;
        }
    }
    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ping_lista, parent, false);

        TextView txtTitulli = row.FindViewById<TextView>    (Resource.Id.titulli);
        TextView txt1 = row.FindViewById<TextView>(Resource.Id.txt1);
        TextView txt2 = row.FindViewById<TextView>(Resource.Id.txt2);
        TextView txt3 = row.FindViewById<TextView>(Resource.Id.txt3);

        MyView view = new MyView(row) { mtitulli = txtTitulli, msekuenca = txt1, mmadhesia = txt2, mkoha = txt3 };
        return view;
    }
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        MyView mHolder = holder as MyView;
        int indexPosition = (mdata.Count - 1) - position;
        mHolder.mtitulli.Text = mdata[position].titulli;
        mHolder.msekuenca.Text = mdata[position].sekuenca;
        mHolder.mmadhesia.Text = mdata[position].madhesia;
        mHolder.mkoha.Text = mdata[position].koha;
    }
    public override int ItemCount
    {
        get { return mdata.Count; }
    }
    public override long GetItemId(int position)
    {
        return base.GetItemId(position);
    }
}
}

如何使这项工作?我尝试了很多东西,现在我被困了:)我还有另一个问题:

如果我更改mAdapter.NotifyItemInserted(mdata.Count()); to mAdapter.NotifyItemInserted(0);将物品放在顶部的Recyclerview上,只需复制物品,不要将新物品放入视野。

提前致谢

2 个答案:

答案 0 :(得分:0)

适配器位置是基于零的索引

更改mAdapter.NotifyItemInserted(mdata.Count())mAdapter.NotifyItemInserted(mdata.Count()-1)

它会自动滚动到最后一项

如果还没有工作的话 同时将mRecyclerView.SmoothScrollToPosition(mdata.Count())更改为 mRecyclerView.SmoothScrollToPosition(mdata.Count()-1)

答案 1 :(得分:0)

试试这个

recyclerView.smoothScrollToPosition(data.size()-1);