关于图像点击问题

时间:2016-03-07 04:47:12

标签: android listview android-viewpager android-imageview

我对图片点击问题有疑问:

我正在使用粘贴的视差效果,我的Viewpager无法点击。 我的要求是,如果用户点击它,那么他应该重定向到另一个页面。我正在使用this链接。

我使用Viewpager而不是Imageview,而Viewpager有一些图片滑动。 那不工作。我正在获得一个图像阵列。

知道我哪里错了吗?

感谢。

 My Code:
 public class MainActivity extends ActionBarActivity {

 private TextView stickyView;
 private ListView listView;
 private View heroImageView;

 private View stickyViewSpacer;

 private int MAX_ROWS = 20;

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

    /* Initialise list view, hero image, and sticky view */
    listView = (ListView) findViewById(R.id.listView);
    heroImageView = findViewById(R.id.heroImageView);
    stickyView = (TextView) findViewById(R.id.stickyView);

    heroImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(CollectionDetailActivity.this, "click",  Toast.LENGTH_SHORT).show();
        }
    });

    /* Inflate list header layout */
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View listHeader = inflater.inflate(R.layout.list_header, null);
    stickyViewSpacer = listHeader.findViewById(R.id.stickyViewPlaceholder);

    /* Add list view header */
    listView.addHeaderView(listHeader);

    /* Handle list View scroll events */
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {

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

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

            /* Check if the first item is already reached to top.*/
            if (listView.getFirstVisiblePosition() == 0) {
                View firstChild = listView.getChildAt(0);
                int topY = 0;
                if (firstChild != null) {
                    topY = firstChild.getTop();
                }

                int heroTopY = stickyViewSpacer.getTop();
                stickyView.setY(Math.max(0, heroTopY + topY));

                /* Set the image to scroll half of the amount that of ListView */
                heroImageView.setY(topY * 0.5f);
            }
        }
    });


    /* Populate the ListView with sample data */
    List<String> modelList = new ArrayList<>();
    for (int i = 0; i < MAX_ROWS; i++) {
        modelList.add("List item " + i);
    }

    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_row, modelList);
    listView.setAdapter(adapter);
 }
 }

1 个答案:

答案 0 :(得分:0)

查看层次结构,ImageView上的ListView和ListView将处理click事件。 ListView上的StickyView。如果需要处理单击事件ImageView,可以使用ListView的单击事件空间项(list_header布局)。

改变你的:

heroImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(CollectionDetailActivity.this, "click",  Toast.LENGTH_SHORT).show();
        }
    });

代码:

    stickyView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "stickyView clicked", Toast.LENGTH_SHORT).show();
        }
    });

并添加此项来处理ImageView的点击事件:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            Toast.makeText(MainActivity.this, "heroImageView clicked", Toast.LENGTH_SHORT).show();
        } else {
            //ListView item clicked
        }
     }
});

Complete code