OnItemClickListener is not firing in listview - android

I am working on a school project, in which i have a main_activity with two fragments. One of these fragments contains a listview. The listview gets filled through an adapter, and this all seems to work fine. However i try to add an OnItemClickListener to this listview, and i really can't get it to fire.
This is the fragment with the listview (overview_fragment.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
//android:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
This where I add the listener in the overviewFragment.java:
public class OverviewFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_fragment, container, false);
final ListView listView = view.findViewById(R.id.list_view);
AdapterView.OnItemClickListener mMessageClickedHandler = new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long
id) {
Log.d(OverviewFragment.class.getSimpleName(), "listener in overviewFragment");
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
return view;
}
Here is the list item i use:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/name"
/>

You should add items and adapter for the on click listener to work. Remove android:descendantFocusability="blocksDescendants", blocksDescendants removes focus for all the children in the ViewGroup.

Related

Listview in the ListViewFragment is not appearing in the activity

I have the activity_main.xml with one FrameLayout relative to a ListViewFragment. I want to show this fragment in this activity. But its not working the ListView is not appearing. Do you know where is the issue?
main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_list"
/>
</android.support.constraint.ConstraintLayout>
Main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// here Im trying to add the fragment A to the activity but the listview is not appearing
getSupportFragmentManager().
beginTransaction().add(R.id.fragment_list,new ListViewFragment()).commit();
}
}
listview xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
ListViewFragment:
public class ListViewFragment extends Fragment {
private ListView editor=null;
ArrayList<String> items = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_list, container, false);
editor=(ListView) (result.findViewById(R.id.listView));
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items);
editor.setAdapter(arrayAdapter);
return result;
}
}
Add height and width to the framelayout.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_list" />
Also your listView Fragment cannot have listview as parent layout.So wrap it with a layout like frame layout.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

android recyclerView horizontal and vertical spacing

Hi all I am very new to android and I am having a problem with recyclerview. I am trying to add space between image views in a recyclerview but I am not successful.
What I want
What is happening
Below are my implementations
ItemOffsetDecoration.java
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int itemOffset;
public ItemOffsetDecoration(int itemOffset) {
itemOffset = itemOffset;
}
public ItemOffsetDecoration(#NonNull Context context, #DimenRes int itemOffsetId) {
this(context.getResources().getDimensionPixelSize(itemOffsetId));
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(itemOffset, itemOffset, itemOffset, itemOffset);
}
}
shopping.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shopping_bg"
tools:context=".activities.HomepageActivity$PlaceholderFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/shoppingRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="#dimen/grid_horizontal_spacing"/>
</RelativeLayout>
Shopping.java
public class Shopping extends Fragment implements InstaPukkeiRecyclerViewAdapter.ItemClickListener {
InstaPukkeiRecyclerViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_shopping, container, false);
processRV(rootView);
return rootView;
}
private void processRV(View layout) {
RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.shoppingRV);
int noOfColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), noOfColumns));
adapter = new InstaPukkeiRecyclerViewAdapter(getContext(), LogoIds.SHOPPING_SITE_LOGOS);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new ItemOffsetDecoration(getContext(), R.dimen.grid_horizontal_spacing));
}
}
Please help me here. Thanks in advance.
EDIT
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:marginLeft="18dp"
android:marginRight="18dp"
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
Change your item layout with this one in your item's XML. You make the parent(LinearLayout) height match parent that's why you are getting vertical full screen space
Use wrap_content in RecyclerView and also in row layout.And also add margins
<android.support.v7.widget.RecyclerView
android:id="#+id/shoppingRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:padding="#dimen/grid_horizontal_spacing"/>
hmmm you can add margin to your image view or use dimen to set height and width something like this
android:layout_width="#dimen/dp_130"
android:layout_height="#dimen/dp_120"
or
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_margin="#dimen/dp_15"
android:adjustViewBounds="true" />
Inside your Recycleview set layout height to wrap_content

Can't see my item on ListView

Java Code:
public class BlankFragment extends Fragment {
public BlankFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_blank, container, false);
ListView listView=(ListView) view.findViewById(R.id.listview);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("example");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
}
XML code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.BlankFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="204dp"
android:id="#+id/listview"
android:background="#d36868"
android:layout_gravity="center"></ListView>
</FrameLayout>
This is my code ... I have initialize a ListView and i tried to add an item called "example" . When i run this app I can see my listview but without any ietm.
There is no problem with the code above.Because I tried.
I think it's some error code that you didn't get the code out of it.Such as Other Fragment,Activity,and so on.
Try to use wrap_content instead of fixed height
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.BlankFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listview"
android:background="#d36868"
android:layout_gravity="center"></ListView>
</FrameLayout>

SlidingPaneLayout does not slide

The slidingPaneLayout does not slide!
pane.isSlideable() is false!
I inflate the layout in this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
slidingView = inflater.inflate(R.layout.available_nodes_fragment, container, false);
SlidingPaneLayout pane = (SlidingPaneLayout)slidingView.findViewById(R.id.sliding_pane_layout);
Fragment fragmentA = new ListNodesFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.child_fragmentA, fragmentA );
Fragment fragmentB = new ReserveNodesFragment();
transaction.add(R.id.child_fragmentB, fragmentB);
transaction.commit();
//pane.setPanelSlideListener(new SliderListener());
pane.openPane();
Log.i("Available Nodes","PANE IS SLIDABLE" + pane.isSlideable());
return slidingView;
}
The available_nodes_fragment layout that i inflate is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="right" />
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
I do not know why the SlidingPaneLayout does not slide. I do not know if this problem has to do with the fact that i use nested fragments.
I have some progress.
I changed the available_nodes_fragment layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reserve_nodes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/child_fragmentA"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="left"
android:layout_marginRight="25dp"
android:background="#A588DC" />
<FrameLayout
android:id="#+id/child_fragmentB"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="#B6D4D3"
android:layout_gravity="right" />
</android.support.v4.widget.SlidingPaneLayout>
<Button
android:id="#+id/btnReserveNodes"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:text="Reserve"/>
</RelativeLayout>
Now, the SlidePaneLayout layout slides. I do not know why. When i changed the fixed layout_width of the FrameLayouts to wrap_content, the SlidePaneLayout started to slide.
My problem now is that i want the FrameLayout with id= "child_fragmentA" with the ListView to get the 30% of the screen horizontally. I did not found a way to do it, since FrameLayout does not support weights.
I also provide the code of ListNodesFragment and ReserveNodesFragment.
ListFragments.java
public class ListNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View listNodesView = inflater.inflate(R.layout.nodes_categories_listview, container, false);
String[] items = { getResources().getString(R.string.orbit_nodes),
getResources().getString(R.string.grid_nodes),
getResources().getString(R.string.usrp_nodes),
getResources().getString(R.string.diskless_nodes),
getResources().getString(R.string.icarus_nodes) ,
getResources().getString(R.string.base_station)
};
ListView nodesListView = (ListView) listNodesView.findViewById(R.id.nodes_list);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.nodes_categories_listview_item, items);
nodesListView.setAdapter(adapt);
return listNodesView;
}
}
ReserveNodesFragment.java
public class ReserveNodesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View reserveNodesView = inflater.inflate(R.layout.reserve_nodes_fragment, container, false);
TextView txtView = (TextView) reserveNodesView.findViewById(R.id.textView1);
txtView.setText("fragment 2");
return reserveNodesView;
}
}

Listview does not display in the fragment of masterdetailsflow

I am using the masterdetailsflow template to build a custom detail layout. it doesn't seem to work well. The closest question asked before does not seem to be solving my problem at
ListView not showing up in fragment
I put a Log to do debug, it seems to pass the "Check1" but it does not displaying.
public class RouteItemDetailFragment extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_routeitem_detail, container, false);
if (mItem != null) {
((TextView) rootView.findViewById(R.id.routeitem_detail)).setText(mItem.title);
Log.i("GPS", "onCreateView - RouteItemDetailFragment - check listadapter");
}
return rootView;
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
ArrayList<String> listItems = new ArrayList<String>();
listItems.add("helo1");
listItems.add("helo2");
listItems.add("helo3");
listItems.add("helo4");
ArrayAdapter<String> adapter;
((TextView) view.findViewById(R.id.txt1)).setText("kkkkkk");
Log.i("Check1", "onCreateView - RouteItemDetailFragment - check listadapter");
adapter=new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
listItems);
adapter.notifyDataSetChanged();
ListView listAdapter = (ListView) view.findViewById(R.id.lv);
listAdapter.setAdapter(adapter);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/twopanecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="?android:attr/textAppearanceLarge"
android:id="#+id/routeitem_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".RouteItemDetailFragment" />
<TextView
android:id="#+id/txt1"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
/>
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Your LinearLayout displays Views horizontally by default, so your second TextView and ListView are probably just being displayed off screen. Change the orientation to "vertical":
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/twopanecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

Categories

Resources