Monday, May 5, 2014

Help: fragment under listview.setOnItemClickListener () void


In the main Activity using fragment implements a tab (tab1), tab1 display data using listview, listview layout of each line in a separate xml definition.
1.xml part of the code
1.1, the definition
main.xml part in tabhostandroid: id = "@ android: id / tabhost"
android: layout_width = "match_parent"
android: layout_height = "match_parent">
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical">
android: id = "@ android: id / tabs"
android: layout_width = "match_parent"
android: layout_height = "wrap_content">

android: id = "@ android: id / tabcontent"
android: layout_width = "match_parent"
android: layout_height = "match_parent">

android: id = "@ + id/tab1"
android: layout_width = "match_parent"
android: layout_height = "match_parent" />



1.2.tab1 the xml definition of
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin">

android: id = "@ + id/listView1"
android: layout_width = "match_parent"
android: layout_height = "match_parent">



1.3.listview defined in xml
each row
android: id = "@ + id/widget32"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: padding = "5dp"
android: descendantFocusability = "blocksDescendants"
android: focusable = "true"
xmlns: android = "http://schemas.android.com/apk/res/android">
android: id = "@ + id / listImageView"
android: layout_width = "50dp"
android: layout_height = "50dp"
android: layout_weight = "1"
android: focusable = "false"
android: clickable = "false"
android: focusableInTouchMode = "false"
android: src = "@ android: drawable / btn_star" />
android: id = "@ + id/widget34"
android: layout_width = "wrap_content"
android: layout_height = "50dp"
android: layout_weight = "9"
android: focusable = "false"
android: clickable = "false"
android: focusableInTouchMode = "false"
android: orientation = "vertical">
android: id = "@ + id/widget35"
android: layout_width = "fill_parent"
android: layout_height = "50dp"
android: layout_weight = "9"
android: focusable = "false"
android: clickable = "false"
android: focusableInTouchMode = "false"
android: orientation = "horizontal">
android: id = "@ + id/textListInfo1"
android: layout_width = "fill_parent"
android: layout_weight = "3"
android: layout_height = "25dp"
android: focusable = "false"
android: clickable = "false"
android: focusableInTouchMode = "false"
android: text = "home appliances" />

android: id = "@ + id/textListInfo2"
android: layout_width = "fill_parent"
android: layout_weight = "3"
android: layout_height = "25dp"
android: focusable = "false"
android: clickable = "false"
android: focusableInTouchMode = "false"
android: text = "machinery" />




2.java code
2.1 main Activity
protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

setupViews ();
}
private void setupViews () {
TabHost tabhost = (TabHost) findViewById (android.R.id.tabhost);
tabhost.setup ();
tabhost.addTab (tabhost.newTabSpec ("tab1") setIndicator ("tab1") setContent (R.id.tab1)..);
tab_fragment1 tab1 = new tab_fragment1 ();
FragmentManager fm = this.getSupportFragmentManager ();
FragmentTransaction ft = fm.beginTransaction ();
ft.add (tab1, "tab1");

ft.commit ();
}
2.1 tab1 code
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.v ("MyFragment1", "OnCreateView .......");
View v = inflater.inflate (R.layout.activity_tab_fragment1, container, false);
listView = (ListView) v.findViewById (R.id.listView1);
adapter = new LoaderAdapter (10, v.getContext (), R.layout.activity_ent001000_list_item, URLS);
listView.setAdapter (adapter);
listView.setOnScrollListener (mScrollListener);
listView.setClickable (true);
listView.setOnItemClickListener (mItemClickListener);
return v;

}
OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener ()
{

@ Override
public void onItemClick (AdapterView arg0, View arg1, int arg2,
long arg3) {
/ / TODO Auto-generated method stub
listView.getSelectedView () setBackgroundColor (Color.GRAY);.
System.out.println ("You click on the first" + arg3 + "items");
}

};

After running in a virtual machine, the mouse does not have any reaction on the listview also detail what has given the information, set breakpoints in the onitemclick can not walk in.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
See if it is a problem with this code, I tested itemclick can respond

listView.getSelectedView () setBackgroundColor (Color.GRAY).;
Reply:
Thank you for your answer first, comment out the code is useless! Is there any place to set permissions, for example Manifest.xml
Reply:
Above the original LISTVIEW when put directly on the Main Activity, (main activity extends activity)
listView.setOnScrollListener (mScrollListener);
OnScrollListener mScrollListener = new OnScrollListener () {

@ Override
public void onScrollStateChanged (AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_FLING:
adapter.setFlagBusy (true);
break;
case OnScrollListener.SCROLL_STATE_IDLE:
adapter.setFlagBusy (false);
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
adapter.setFlagBusy (false);
break;
default:
break;
}
adapter.notifyDataSetChanged ();
}
@ Override
public void onScroll (AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

}
};
scrolllistener can perform, listview in detail can be scrolled.
But the listview move fragment, main activity extends activity becomes the main activity extends FragmentActivity
listView.setOnScrollListener (mScrollListener);
Also can not perform, nor error.
Reply:
Or call onCreateView in onCreate called in /
Reply:
onCreateView

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.v ("MyFragment1", "OnCreateView .......");
View v = inflater.inflate (R.layout.activity_tab_fragment1, container, false);
listView = (ListView) v.findViewById (R.id.listView1);
adapter = new LoaderAdapter (10, v.getContext (), R.layout.activity_ent001000_list_item, URLS);
listView.setAdapter (adapter);
listView.setOnScrollListener (mScrollListener);
listView.setClickable (true);
listView.setOnItemClickListener (mItemClickListener);
return v;

}
Reply:
Will the getItem method and adapter on ah
corresponding listviewpublic Object getItem (int position) {
return position;
}
In this method, set breakpoints can not walk in.
Reply:
The reason seems to be found in the fragment to use nested getChildFragmentManager () this method, but I now android-support-v13.jar added to the project or compile, but after that there is no method.

No comments:

Post a Comment