如何计算哪个视图占据屏幕的一半以上

时间:2018-10-17 17:26:04

标签: android android-recyclerview android-view recycler-adapter

我有一个recyclerview,上面有3个自定义视图。 RV对于水平方向是可滚动的,每个视图的宽度等于屏幕的宽度。我想显示当前正在显示哪个视图(哪个视图占屏幕的一半以上)。该怎么做?

MainScreenActivity.class

public class MainScreenActivity extends BaseActivity implements View.OnClickListener, MainScreenView {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = findViewById(R.id.recycler_view);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

MyAdapter.class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<ChartDefiner> chartDefiners;
    private DataView dataView;

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        ChartView chartView;

        public MyViewHolder(ChartView chartView) {
            super(chartView);
            this.chartView = chartView;
        }
    }

    public MyAdapter(List<ChartDefiner> chartDefiners, DataView dataView) {
        this.chartDefiners = chartDefiners;
        this.dataView = dataView;
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ChartView v = (ChartView) LayoutInflater.from(parent.getContext()).inflate(R.layout.chart_layout, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.chartView.initData();
    }


    @Override
    public int getItemCount() {
        return chartDefiners.size();
    }

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中实现了一些基本的滚动动画。我使用了这段代码,它运行正常。

render() {
    return (
      <div>
        {<Avatar avatarurl={favouritePersonData[0].avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/>}
      </div>
    )
}