So guys my question is simple, i'm migrating my app layout to android tabview.
I had all the code of the switches and textviews inside my activity_main.xml layout file.
Now i made 3 layout fragments resource xml files and i migrated everything there, plus i included the layout app bar in the activity_amin file
I left all the java codes of the layout elements inside MainActivity.java without changing anything.
Now the app crashes becouse all the layout elements declared inside the fragment layout files return null.
Do i have to set the layout elemnts codes inside the frgament java associated files?
Thank you
You should definitely use the Fragment.java-classes for addressing the gui-components of a fragment.
If you need a refresh for how to work with fragments you can read thru the codelab here: https://codelabs.developers.google.com/codelabs/advanced-android-training-fragments/index.html?index=..%2F..index#0
Best
Sebi
That's simple you probably working inside onCreateView of the fragment
try to work inside the onActivityCreate, because onCreateView the view is not ready and every thing will return null, so if you work on :
Kotlin:
override fun onActivityCreated
Java :
#Override
public void onActivityCreated
that dosen't happen.
I have a RecyclerView and two Tabs in my MainActivity. I just want to show another data in RecyclerView when each Tab gets selected. for doing this should I use ViewPager for Tabs? should I use Fragment? I really don't know. Are they needed in my example? the only difference between tabs is just the data they show to user inside RecylerView. RecyerView is the same. Toolbar is the same , etc. Can anyone help me? Thanks
First off yes you will need a ViewPager. This ViewPager will require you to use Fragments. Take a look at this example from the official docs.
To solve your RecyclerView problem you have two options:
define a custom RecyclerView (by extending Androids RecyclerView)
defining a custom RecyclerViewAdapter and use it with a RecyclerView
I would advise you to do the first if the RecyclerViews are completely similar. Then you could define all your properties with it, without having to do that twice in your Tab Fragment. Otherwise take the second option.
Have you looked at using Conductor?
Conductor
Why I choose Conductor over Fragment?
It is a cool replacement for Fragments.
I'm basically trying to disable scroll on the list view when the user touches the List Header and MotionEvent.ACTION_MOVE is performed.
If you are wondering on why this crazy implementation? - I'm basically building a custom seekbar on my listView Header which works on MotionEvent.ACTION_MOVE and since the view is a header of list view it should not scroll until user takes off touch from this custom header view.
So my question/problem is.
For now i have 2 java classes. Activity (which has this list view) and the custom Header (which is a View implementing OnTouchListener).
How do I pass value when my header view is touched to this list view (activity) to disable scrolling?
It seems that list view scroll can be disabled by overriding dispatchTouchEvent as said here. Since mine is not a custom listView is it possible to disable in activity class itself?
Sorry i'm just learning, so please dont mind my newbiness.
Ok, I have gone little quiet far on the question I asked, So I'm answering the first half of my question.
How do I pass value when my header view is touched to this list view (activity) to disable scrolling?
I got my solution in creating an interface. So I created a public static interface which has a public void headerTouched(Boolean touchEvent); method. My class which has the code for the listView is the implementer so it automatically overrides this method from the interface class. I this class I create a private variable (in my case: boolean) which listens for the boolean value returned from the driver class. My driver class is the HeaderView which has this interface object and through it I set the value. To help newbies on interface, I have posted my code below.
Interface Class
public static interface HeaderTouchListener{
public void headerTouched(Boolean touchEvent);
}
Implementer Class
private boolean headerTouched = false;
public void headerTouched(Boolean touchEvent) {
headerTouched = touchEvent;
}
Driver Class
HeaderTouchListener touchListner = new ImplementerClassName();
touchListner.headerTouched(true);
I'm still having trouble figuring out how to disable scroll on my list view. More information on it, please follow this question Set dispatchTouchEvent for List View without creating custom List View class. (for disabling scroll)
I'm trying implement "pull to refresh" on a ListFragment but right now none of the drop in libraries seem to support it. There's no way to detect overscroll on the list fragment that I can see so I'm wondering if anyone has found a means to get this working?
--
Using Christian's tip I used the following for my onCreateView() method.
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
PullToRefreshListView listView = new PullToRefreshListView(getActivity());
mAdapter = new HomeTweetListAdapter(getActivity(), R.layout.tweet_list_item, tweets);
listView.setAdapter(mAdapter);
return listView;
}
Like Christian said you can only do this with a Fragment. Returning anything other than a ListView on a ListFragment errors out.
EDIT:
To clarify I am using Johan's PullToRefresh library
I actually make it work using fragments (not ListFragment). So it is basically the same, just return the PullToRefreshListView from your onCreateView method and that's it. It should also work with ListFragment; remember that you must return a ListView from onCreateView if you use ListFragment (you can return whatever you want if you use just Fragment).
Here is a Component that Johan has created that adds a pull down to refresh feature..
Pull-To-Refresh
Main logic is implemented in PullToRefreshListView that extends ListView.
n your layouts you simply add it like this.
<com.markupartist.android.widget.PullToRefreshListView
android:id="#+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
It is now fully documented how to use ActionBar-PullToRefresh together with ListFragment:
https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/ListFragment
Hope that helps!
I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListener was disabled somehow on my ListView rows, because I have an ImageButton in the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.
I've checked this question, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.
Instead of an OnItemClickListener, add an OnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list:
ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);
and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(context, R.layout.cell, null);
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(context).setTitle("touched").show();
}
});
return view;
}
set your ImageButton's attribute:
android:focusable="false"
Because AbsListView.onTouchEvent check child.hasFocusable().
I've tested the following solution on SDK levels 8 and 16.
In getView()
setFocusable(false);
setClickable(false);
rather than setting them true in the Adapter's getView() does what I think the original question wanted, and means that an OnItemClickListener gets called, provided that an OnClickListener is not set in getView().
I'm assuming that anything you can do in an View's OnClickListener you can do just as easily in a ListView's OnItemClickListener.
(setOnClickListener on a View implicitly sets the view to be clickable, which prevents the ListView's corresponding OnItemClickListener getting called, apparently.)
The behaviour is as one would expect, in terms of the ImageButton's visual state when the item is pressed or rolled over.
The solution is a slight illusion, in that it is the list item that's being pressed not the ImageButton itself, so if the button doesn't occupy whole list item, clicking somewhere else in the item will still make the button's drawable state reflect the click. Same for focus. That might be a price worth paying.
This will definitely work. Add this to the layout definition.
android:descendantFocusability="blocksDescendants"
Found the solution here
One alternative to setting an OnClickListener for every view is to NOT use an ImageButton - use an ImageView instead. The ImageView can still send events to an OnClickListener and won't take over the focus.
best way to do is this:
android:focusable="false"
android:focusableInTouchMode="false"
set these properties for that Imagebutton and try.
I
For my version of this problem, the issue was that I had set my TextView object to android:inputType="textMultiLine". When I removed this line the issue of the list not being clickable was gone. Looks like a nasty little bug.
Also, I'm still able to use the android:minLines/android:maxLines properties with no problem, so it's not a big issue. Just not the solution I expected.
The following line solved the issue in my project:
<TextView ... android:textIsSelectable="false" />
As an alternative solution which worked for me you can try to extend your adapter from BaseAdapter (iso implementing ListAdapter interface)
Put This code ImageView nextpage= (ImageView)findViewById(R.id.btnEdit); instead of ImageButton . now the list item is active
I have sub-classed ImageButton and setFocusable="false" in layout definition didn't work for me. It solved calling setFocusable(false) in constructor of subclass.
Using a ScrollView can prevent the onItemClickListener from receiving the input.
Hope this helps anyone.