Sliding between multiple views using custom adapter

I am trying trying to create a gallery application that slides between three different xml layouts. Application looks quite wrong:

My XML files are:

linear.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="one"/>
<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="two"
        android:gravity="center"
        android:layout_weight="1"/>
<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="three"
        android:layout_gravity="right"/>
</LinearLayout>

table.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<TextView android:text="What is your name?"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:gravity="center"/>
<TableRow>
    <TextView android:text="@string/firstname"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content" />
    <EditText android:text="John"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content" />
</TableRow>
<TableRow>
    <TextView android:text="@string/lastname"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content" />
    <EditText android:text="Poe"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content" />
</TableRow>
</TableLayout>

and the gallery.xml:
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

My implementation of BaseAdapter:

public class LayoutsAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public LayoutsAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public Object getItem(int i) {
        switch (i) {
            case 0:
                return R.layout.linear;
            case 1:
                return R.layout.table;
            case 2:
                return R.layout.relative;
            default:
                return -1;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        convertView = mInflater.inflate((Integer)getItem(i), null);
        return convertView;
    }
}

I am very new to android, so I am looking forward for your help.

Answers


I think you should use ViewPager. Checkout the following information on ViewPager:

http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

Also consider that you might want to use Fragments in combination with ViewPager.

But ViewPager I think is what you are looking for. In fact its much smoother than doing you own layout stuff.


I would recommend you to use Jake Whartons ViewPageIndicator(or just a normal ViewPager):

Create a Fragment for every of your Layouts and set the layout to your Fragment with the onCreateView() method inside your fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        return inflater.inflate(R.layout.layout, container, false);
    }

Now create a FragmentPagerAdapter there should be a method called getItem(). Switch the Position and set it to your Fragment:

    @Override public Fragment getItem ( int position){

        switch (position) {
            case 0:
                TestFragment fragment = new TestFragment();
                return fragment;

            case 1:
                TestFragment2 fragment2 = new TestFragment2();
                return fragment2;
        }

        return defaultFragment fragment3 = new defaultFragment();
        return fragment3;
    }

Now you should be able to swipe to your Layouts(Fragments) easily


Need Your Help

Equivalent of .bat in mac os

osx batch-file apple sh

I have a .bat file to invoke a java file , My question is wat changes should be made , to execute the same in MAC OS , .bat format should be changed to .sh i guess , or the below stmts remains the ...

Oracle Materialized View: update MVs in the remote db?

oracle materialized-views

I created a fast refresh MV in a remote db by using a syntax like follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.