Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.customhorizontalscrollviewexample;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener
- {
- private static final int ITEM_ID = 4500;
- private LinearLayout rootLayout;
- private ArrayList<DataModel> dataModels;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setUIRef();
- bindHZSWLayoutData();
- }
- private void setUIRef()
- {
- rootLayout = (LinearLayout) findViewById(R.id.Layout_Root);
- }
- private void bindHZSWLayoutData()
- {
- dataModels = new ArrayList<>();
- dataModels.add(new DataModel("Apple", R.drawable.apple));
- dataModels.add(new DataModel("Banana", R.drawable.banana));
- dataModels.add(new DataModel("Cherry", R.drawable.cherry));
- dataModels.add(new DataModel("Kiwi", R.drawable.kiwi));
- dataModels.add(new DataModel("Mango", R.drawable.mango));
- dataModels.add(new DataModel("Orange", R.drawable.orange));
- dataModels.add(new DataModel("Pineapple", R.drawable.pineapple));
- dataModels.add(new DataModel("Strawberry", R.drawable.strawberry));
- for (int i = 0; i < dataModels.size(); i++)
- {
- LinearLayout itemLayout = new LinearLayout(MainActivity.this);
- itemLayout.setId(ITEM_ID+i);
- itemLayout.setOrientation(LinearLayout.VERTICAL);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- params.gravity = Gravity.CENTER;
- params.setMargins(10, 10, 10, 10);
- itemLayout.setLayoutParams(params);
- ImageView imageView = new ImageView(MainActivity.this);
- imageView.setLayoutParams(params);
- imageView.setImageResource(dataModels.get(i).getResId());
- TextView textView = new TextView(MainActivity.this);
- textView.setLayoutParams(params);
- textView.setText(dataModels.get(i).getName());
- itemLayout.addView(imageView);
- itemLayout.addView(textView);
- itemLayout.setOnClickListener(this);
- rootLayout.addView(itemLayout);
- }
- }
- @Override
- public void onClick(View view)
- {
- if (!checkRootLayoutItemClick(view))
- {
- // other view clicked
- }
- }
- private boolean checkRootLayoutItemClick(View view)
- {
- boolean flag = false;
- for (int i = 0; i < rootLayout.getChildCount(); i++)
- {
- View viewChild = rootLayout.getChildAt(i);
- if (viewChild instanceof LinearLayout)
- {
- if (viewChild == view)
- {
- flag = true;
- Toast.makeText(this, "item "+i+" clicked", Toast.LENGTH_LONG).show();
- }
- }
- }
- return flag;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement