2016-03-07 67 views
1

我有一個關於圖片點擊問題的問題:關於圖片點擊問題

我使用了粘粘的視差效果,在此我Viewpager是不工作的點擊。 我的要求是,如果用戶點擊,那麼他應該重定向到另一個頁面。我正在使用this鏈接。

我使用Viewpager而不是Imageview,而Viewpager有一些圖像滑動。 多數民衆贊成沒有Working.I得到一個數組的圖像。

任何想法,我錯了嗎?

謝謝。

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); 
} 
} 
+0

你加點擊事件Imageview? –

+0

是的,我認爲它的效果就是爲什麼它不點擊 –

+0

在這裏發表你的代碼。 –

回答

0

檢查並查看有關ImageView的層次結構,ListView和單擊事件將是由ListView的處理。 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

+0

我使用Viewpager而不是Imageview,那我該如何管理這個場景呢? –