我正在做什么 ::我有一个适配器,我在其中动态添加视图
我想做什么 ::我正在尝试检测动态创建的onclick
imageView
MainActivity.java
public class MainActivity extends Activity {
TableLayout table_layout = null;
private List<Map<String,CharSequence>> mListLayoutData=new ArrayList<Map<String,CharSequence>>();
private Map<String, CharSequence> objMap = new HashMap<String, CharSequence>();
private HFGridView gridView;
private int screenWidth=0;
private int screenHeight=0;
Configuration newConfig;
int numOfColumns=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (HFGridView) findViewById(R.id.gridView);
table_layout = (TableLayout)findViewById(R.id.tableLayout);
gridView.setNumColumns(1);
//Get the Width and the height of the screeen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//Portrait Mode
numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
gridView.setColumnWidth(metrics.widthPixels);
}
else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//Landscape Mode
numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
gridView.setColumnWidth(metrics.widthPixels);
}
View header1 = getHeader("Header");
View footer1 = getFooter("Footer");
gridView.addHeaderView(header1);
gridView.addFooterView(footer1);
objMap.put("Starter","HEADER");
mListLayoutData.add(objMap);
if(numOfColumns==3){
objMap = new HashMap<String, CharSequence>();
objMap.put("1","starter");
objMap.put("2","starter");
objMap.put("3","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("4","starter");
objMap.put("5","starter");
mListLayoutData.add(objMap);
}else{
objMap = new HashMap<String, CharSequence>();
objMap.put("1","starter");
objMap.put("2","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("3","starter");
objMap.put("4","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("5","starter");
mListLayoutData.add(objMap);
}
gridView.setListener(new HFGridView.HFGridViewListener() {
@Override
public void readyToDisposeItems() {
gridView.setAdapter(adapter);
}
});
}
private BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
//return 1;
return mListLayoutData.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
TableLayout table_layout;
ImageView imageView = null;
TableRow table_row = null;
LinearLayout linearLayout=null;
if (view == null) {
//Create the tableLayout and add params to it
table_layout=new TableLayout(MainActivity.this);
table_layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//table_layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
} else {
table_layout=(TableLayout) view;
}
final Map<String, CharSequence> objMap= mListLayoutData.get(position);
//Create the tableRow and add params to it
table_row = new TableRow(MainActivity.this);
table_row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
//linearLayout.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
// inner for loop
for (Entry<String, CharSequence> entry : objMap.entrySet()) {
String key = entry.getKey();
CharSequence value = entry.getValue();
if(value=="HEADER"){
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
textView.setText(key);
textView.setTextSize(20);
textView.setTextColor(getResources().getColor(R.color.redColor));
textView.setPadding(5, 5, 5, 5);
table_row.addView(textView);
}else{
//Create the Linear Layout and add params to it
linearLayout=new LinearLayout(MainActivity.this);
linearLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
//Create the ImageView and add params to it
imageView = new ImageView(MainActivity.this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(160, 100));
//imageView.setBackgroundDrawable(imageView.getResources().getDrawable(R.drawable.eight));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.eleven);
//Create the TextView and add params to it
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText(key);
textView.setTextSize(12);
textView.setTextColor(getResources().getColor(R.color.redColor));
linearLayout.addView(imageView);
linearLayout.addView(textView);
table_row.addView(linearLayout);
}
}
table_layout.addView(table_row);
return table_layout;
}
};
private View getHeader(String text) {
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView textView = (TextView) header.findViewById(R.id.headerTextView);
textView.setText(text);
return header;
}
private View getFooter(String text) {
View footer = getLayoutInflater().inflate(R.layout.footer, null);
TextView textView = (TextView) footer.findViewById(R.id.footerTextView);
textView.setText(text);
return footer;
}
}
答案 0 :(得分:0)
找到我的答案的解决方案!
This Link和comment帮助我找到解决方案,分享它以便对某人有所帮助!
public class MainActivity extends Activity {
TableLayout table_layout = null;
private List<Map<String,CharSequence>> mListLayoutData=new ArrayList<Map<String,CharSequence>>();
private Map<String, CharSequence> objMap = new HashMap<String, CharSequence>();
private HFGridView gridView;
private int screenWidth=0;
private int screenHeight=0;
Configuration newConfig;
int numOfColumns=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (HFGridView) findViewById(R.id.gridView);
table_layout = (TableLayout)findViewById(R.id.tableLayout);
gridView.setNumColumns(1);
//Get the Width and the height of the screeen
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//Portrait Mode
numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
gridView.setColumnWidth(metrics.widthPixels);
}
else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//Landscape Mode
numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
gridView.setColumnWidth(metrics.widthPixels);
}
View header1 = getHeader("Header");
View footer1 = getFooter("Footer");
gridView.addHeaderView(header1);
gridView.addFooterView(footer1);
objMap.put("Starter","HEADER");
mListLayoutData.add(objMap);
if(numOfColumns==3){
objMap = new HashMap<String, CharSequence>();
objMap.put("1","starter");
objMap.put("2","starter");
objMap.put("3","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("4","starter");
objMap.put("5","starter");
mListLayoutData.add(objMap);
}else{
objMap = new HashMap<String, CharSequence>();
objMap.put("1","starter");
objMap.put("2","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("3","starter");
objMap.put("4","starter");
mListLayoutData.add(objMap);
objMap = new HashMap<String, CharSequence>();
objMap.put("5","starter");
mListLayoutData.add(objMap);
}
gridView.setListener(new HFGridView.HFGridViewListener() {
@Override
public void readyToDisposeItems() {
gridView.setAdapter(adapter);
}
});
}
private BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
//return 1;
return mListLayoutData.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
TableLayout table_layout;
ImageView imageView = null;
TableRow table_row = null;
LinearLayout linearLayout=null;
if (view == null) {
//Create the tableLayout and add params to it
table_layout=new TableLayout(MainActivity.this);
table_layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//table_layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
} else {
table_layout=(TableLayout) view;
}
final Map<String, CharSequence> objMap= mListLayoutData.get(position);
//Create the tableRow and add params to it
table_row = new TableRow(MainActivity.this);
table_row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
//linearLayout.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
// inner for loop
for (Entry<String, CharSequence> entry : objMap.entrySet()) {
String key = entry.getKey();
CharSequence value = entry.getValue();
if(value=="HEADER"){
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
textView.setText(key);
textView.setTextSize(20);
textView.setTextColor(getResources().getColor(R.color.redColor));
textView.setPadding(5, 5, 5, 5);
table_row.addView(textView);
}else{
//Create the Linear Layout and add params to it
linearLayout=new LinearLayout(MainActivity.this);
linearLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
//Create the ImageView and add params to it
imageView = new ImageView(MainActivity.this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(160, 100));
//imageView.setBackgroundDrawable(imageView.getResources().getDrawable(R.drawable.eight));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.eleven);
//Create the TextView and add params to it
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText(key);
textView.setTextSize(12);
textView.setTextColor(getResources().getColor(R.color.redColor));
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Clicked !", Toast.LENGTH_LONG).show();
}
});
linearLayout.addView(imageView);
linearLayout.addView(textView);
table_row.addView(linearLayout);
}
}
table_layout.addView(table_row);
return table_layout;
}
};
private View getHeader(String text) {
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView textView = (TextView) header.findViewById(R.id.headerTextView);
textView.setText(text);
return header;
}
private View getFooter(String text) {
View footer = getLayoutInflater().inflate(R.layout.footer, null);
TextView textView = (TextView) footer.findViewById(R.id.footerTextView);
textView.setText(text);
return footer;
}
}