How to set primary item after update to viewpager2 and FragmentStateAdapter? - android-studio

I'm looking into an opensource project on github and it still currently makes use of ViewPager and FragmentStatePagerAdapter.
I'm trying to update it to use ViewPager2 and FragmentStateAdapter but there is some functions that I'm not sure about
public void setPrimaryItem(#NonNull ViewGroup container, int position, #NonNull Object object)
This was available as an override for viewpager but isn't anymore.
Any help?

Related

Android Studio; What is the point of inflating and saving a view into convertView?

#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = ContactViewActivity.this.getLayoutInflater().inflate(R.layout.contact_view_field_row,
parent, false);
}
String value = (String)getItem(position);
TextView fieldValue = (TextView)convertView.findViewById(R.id.contact_view_row_value);
fieldValue.setText(value);
return convertView;
}
The above code is taken from a BaseAdapter class. So what is the point of setting the inflated view to convertView? The whole point of the getView is to make a view if there isn't one, give it attributes, then return it so it can be shown in the application in the ListView right? So why not just say findViewById(R.id.contact_view_row_value);? I mean what are we really doing with the layoutInflater? I'd think that since we set it equal to convertView, there would be no need to type convertView.findViewById. I mean it is already equal to an inflated XML file right? I'm quite confused here, I'd really appreciate some clarification.
Because convertView is something that can be reused, though at runtime you don't know whether you're being provided an existing View whose data must be updated, or if a new View needs to be inflated.
When your application starts, you need to inflate your Views in order for any data to be displayed in your ListView at all. Inflate means to parse the resource XML into a graphical object that and provide it with Android's Context, so it becomes something that has the potential to be rendered and interacted with on the graphical hierarchy. Later on in the application, Android tries to help you save resources and avoid unnecessary inflation by passing you a View that had been inflated earlier; that's why we check if convertView is not equal to null. If it's not, you can convert that existing View into something that displays data relevant for the current index.
However, it's worth noting that the ListView is now deprecated. Try using the RecyclerView instead, where this behaviour is a little more obvious. Here's an example.

from an activity to new xml file without making new class for the xml

I want to know if threre is a way to use a button.OnclickListener() to go from one activity to xml file without making a class for that xml.For example:
btn.OnClickListener(new OnclickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(ex.this, ex.class);
startActivity(i);
}};
we use the intent to go from activity class to another new activity class, and in the new class there is setContentView(R.layout.example); in onCreate method, so the xml layout will be displayed.
but I ask if there is a way to display the xml layout withot making a class activity.
There isn't if want to go to a new activity I believe you need a class. You can do this programatically without any XML.
(however I am reasonably new to android so could be wrong)

How to get rid of title bar in android studio

I am a very new programmer and I have just finished my first android app, I have a problem with the title bar. Ive googled and searched on stack overflow that if I changed the theme into notitlebar_ i will not have a title bar. However while that is true using the xml view, it is still present on the emulator, and also present when i ran it on my phone. (I also tried android:theme="#android:style/Theme.Black.NoTitleBar", but with the same results)
so how would i solve this? thanks in advance!!
Try something like this on the onCreate
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Here's the way that works best for me:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide the titlebar
getSupportActionBar().hide();
setContentView(R.layout.activity_main);

How to display multiple ListViews in one activity

In my application I need to display multiple (three) listviews at a time sections wise. Can anyone suggest me a best way to implement this.
Thanks in advance,
Chandra.
Maybe you want to look into fragments.
http://developer.android.com/guide/components/fragments.html
if you create the ListView's in an XML file, you can just specify the ID attribute like so: android:id="#+id/listView1, providing a different ID for each ListView. In your Java code, you will want to extend Activity and create three ListView objects and point them towards the IDs in your XML file. Once you have a handle on the ListView's, you want to create a data source and ArrayAdapter<String> for each ListView. I prefer to use ArrayList<String> over the convential String[] simple because, to me, they are easier to use. A working Java sample below would work for a single ListView. Duplicate the variables and objects twice more with differing names for the two other ListViews. Hope this helps:
public class MainListActivityExample extends Activity {
ListView listView1;
ArrayList<String> lvContents1 = new ArrayList<String>;
ArrayAdapter<String> lvAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Tell the method which layout file to look at
setContentView(R.layout.listViewExample_activity);
// Point the ListView object to the XML item
listView1 = (ListView) findViewById(R.id.listView1);
// Create the Adapter with the contents of the ArrayList<String>
lvAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lvContents1);
// Attach the Adapter to the ListView
listView1.setAdapter(lvAdapter1);
// Add a couple of items to the contents
lvContents1.add("Foo");
lvContents1.add("Bar");
// Tell the adapter that the contents have changed
lvAdapter1.notifyDataSetChanged();
}
In order to add the other two ListViews, create two more ListView objects, two more ArrayList<String> objects, and two more ArrayAdapter<String> objects, each with corresponding names so you know which belongs to which. You can then follow the exact same steps to initialize them.

How do you add a custom UIViewController to existing XIB?

I am just getting my feet wet with MonoTouch and have the most basic of questions. In their tutorials, they cover adding controls to the XIB directly through XCode's Interface Builder. However, I cannot figure out how to add a custom control to the XIB.
I have seen a number of examples of subclassing the base UICollectionView to customize it for your own purposes, and have done this in C# code in my project. My question is, how do I add that specific subclass of the UICollectionView to the XIB for use in the project?
For a normal UIView, the basic steps you need to go through are:
Create your custom view in C# as a class
public class MyView
{
}
Add the UIView base class to it, add a Register attribute and add two constructors:
[Register("MyView")]
public class MyView : UIView
{
public MyView() {}
public MyView(IntPtr handle) : base(handle) {}
}
To make it do something useful, then add a Draw implementation:
public override Draw(RectangleF rect)
{
var context = UIGraphics.CurrentGraphics();
UIColor.Red.SetFill();
context.FillEclipseInRect(rect);
}
Save and Build your project
Now in the XIB editor for the UIViewController in which you want to use your custom view, add a UIView to the design surface
Select that UIView and in the Identity Inspector, set the UIView's "Custom Class" to "MyView"
Save everything in xCode
Return to MonoDevelop, build and run
There's a video of this flow available at:
- http://www.youtube.com/watch?v=ggwO46dd-50&feature=youtube_gdata
For a custom UICollectionView, UILabel, UITableViewCell, or any other UIView base class, then you follow similar steps, just with a different base class and with different constructors too in order to support the specific View.
For a video about custom Table cells, see: http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

Resources