Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package com.example.customhorizontalscrollviewexample;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import java.util.ArrayList;
  14.  
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener
  16. {
  17.  
  18. private static final int ITEM_ID = 4500;
  19. private LinearLayout rootLayout;
  20. private ArrayList<DataModel> dataModels;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState)
  23. {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26.  
  27. setUIRef();
  28. bindHZSWLayoutData();
  29.  
  30. }
  31.  
  32. private void setUIRef()
  33. {
  34. rootLayout = (LinearLayout) findViewById(R.id.Layout_Root);
  35. }
  36.  
  37. private void bindHZSWLayoutData()
  38. {
  39. dataModels = new ArrayList<>();
  40.  
  41. dataModels.add(new DataModel("Apple", R.drawable.apple));
  42. dataModels.add(new DataModel("Banana", R.drawable.banana));
  43. dataModels.add(new DataModel("Cherry", R.drawable.cherry));
  44. dataModels.add(new DataModel("Kiwi", R.drawable.kiwi));
  45. dataModels.add(new DataModel("Mango", R.drawable.mango));
  46. dataModels.add(new DataModel("Orange", R.drawable.orange));
  47. dataModels.add(new DataModel("Pineapple", R.drawable.pineapple));
  48. dataModels.add(new DataModel("Strawberry", R.drawable.strawberry));
  49.  
  50. for (int i = 0; i < dataModels.size(); i++)
  51. {
  52. LinearLayout itemLayout = new LinearLayout(MainActivity.this);
  53. itemLayout.setId(ITEM_ID+i);
  54. itemLayout.setOrientation(LinearLayout.VERTICAL);
  55.  
  56. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  57. params.gravity = Gravity.CENTER;
  58. params.setMargins(10, 10, 10, 10);
  59.  
  60. itemLayout.setLayoutParams(params);
  61.  
  62. ImageView imageView = new ImageView(MainActivity.this);
  63. imageView.setLayoutParams(params);
  64. imageView.setImageResource(dataModels.get(i).getResId());
  65.  
  66. TextView textView = new TextView(MainActivity.this);
  67. textView.setLayoutParams(params);
  68. textView.setText(dataModels.get(i).getName());
  69.  
  70. itemLayout.addView(imageView);
  71. itemLayout.addView(textView);
  72.  
  73. itemLayout.setOnClickListener(this);
  74.  
  75. rootLayout.addView(itemLayout);
  76. }
  77.  
  78. }
  79.  
  80. @Override
  81. public void onClick(View view)
  82. {
  83. if (!checkRootLayoutItemClick(view))
  84. {
  85. // other view clicked
  86. }
  87. }
  88.  
  89. private boolean checkRootLayoutItemClick(View view)
  90. {
  91. boolean flag = false;
  92.  
  93. for (int i = 0; i < rootLayout.getChildCount(); i++)
  94. {
  95. View viewChild = rootLayout.getChildAt(i);
  96.  
  97. if (viewChild instanceof LinearLayout)
  98. {
  99. if (viewChild == view)
  100. {
  101. flag = true;
  102. Toast.makeText(this, "item "+i+" clicked", Toast.LENGTH_LONG).show();
  103. }
  104. }
  105. }
  106.  
  107. return flag;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement