Archive

Archive for May, 2013

How to create Swiping Windows in Android from Android 2.1 onwards?

Have you seen the Google Play application in yourAndroid phone. Wondered about how they implemented swiping windows in it.
Don’t Worry, Here is an example which implements it.

Swipe Fragments in Android

Swipe Fragments in Android

Swipe Fragments in Android

Here we will create three pages which can be swiped to access it.

These are the three layouts for the three sections.

section1.xml

1
2
3
4
5
6
7
8
9
10
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout 1" />
</LinearLayout>

section2.xml

1
2
3
4
5
6
7
8
9
10
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout 2" />
</LinearLayout>

section3.xml

1
2
3
4
5
6
7
8
9
10
11
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout 3" />
</LinearLayout>

Now the java for these layouts. These are also similar , only change is the layout.

Page1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.example.swipewindows;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page1 extends Fragment {
    Context c;
        public Page1(){
        
    }
    public Page1(Context c) {
        this.c = c;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.section1, null);
        return       v;
    }
}

Page2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.swipewindows;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page2 extends Fragment {
    Context c;
        public Page2(){
        
    }
    public Page2(Context c) {
        this.c = c;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.section2, null);
        return v;
    }
}

Page3.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.swipewindows;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page3 extends Fragment {
    Context c;
        public Page3(){
        
    }
    public Page3(Context c) {
        this.c = c;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.section3, null);
        return v;
    }
}

Now the MainActivity.java that uses these sections to join together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.example.swipewindows;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
    /**
     * The android.support.v4.view.PagerAdapter that will provide fragments for
     * each of the sections. We use a
     * android.support.v4.app.FragmentPagerAdapter derivative, which will keep
     * every loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * android.support.v4.app.FragmentStatePagerAdapter.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;
    /**
     * The ViewPager that will host the section contents.
     */
    ViewPager mViewPager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_act);
        // Create the adapter that will return a fragment for each of the three
        // primary sections
        // of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(this,
                getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    /**
     * A FragmentPagerAdapter that returns a fragment corresponding to one of
     * the primary sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        Context c;
        public SectionsPagerAdapter(Context c, FragmentManager fm) {
            super(fm);
            this.c = c;
        }
        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            if (i == 0) {
                fragment = new Page1(c);
            }
            if (i == 1) {
                fragment = new Page2(c);
            }
            if (i == 2) {
                fragment = new Page3(c);
            }
            return fragment;
        }
        @Override
        public int getCount() {
            return 3;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }
}

Now the Strings.xml that contains the section titles.

1
2
3
4
5
6
7
8
9
10
11
<resources>
    <string name="app_name">SwipeWindows</string>
    <string name="title_section3">Section 3</string>
    <string name="title_section2">Section 2</string>
    <string name="title_section1">       Section 1      </string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">     Settings   </string>
    <string name="title_activity_main">SwipeActivity</string>
</resources>

From : http://www.coderzheaven.com/2012/11/28/create-swiping-windows-android-android-2-1-onwards/

Advertisements
Categories: Android

ViewHolder Pattern – Caching View Efficiently

A – Yeah! It works!

Yo, we’ve got a list view to display something:

Sample ListView

Sample ListView

This is what people like to write for list adapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_list_item, null);
    }
    TextView txtName = (TextView)v.findViewById(R.id.txtName);
    TextView txtMail = (TextView)v.findViewById(R.id.txtMail);
    Contact entry = mList.get(position);
    txtName.setText(entry.getName());
    txtMail.setText(entry.getMail());
    return v;
}

That’s great because it works with no problem and the list displays contents nicely!!!

B – Dig It Out

Let’s see how it works..

+ First time loaded, oh right, it’s null!  Ok, to find our TextView controls, ‘findViewById()‘ is loaded. Great, we’ve got it!

+ Second time loaded,  it must be not null, but ‘findViewById()‘ is loaded. Well, just one time, it’s fine!

+ Next time loaded, it’s absolutely not null, eh, ‘findViewById()‘ is called by default. Eh oh..

+ ……whatever time

+ xxx th time…..something is calling ‘findViewById()‘…so stupid, damn it! But it’s still running fine … _ _!

C – Giving birth to a child: ViewHolder

You may see the problem, too. When the list is too big, containing lots of rows, then the calling of ‘findViewById()‘ everytime will impact slightly to your application performance, even though, it’s still working fine; however, it’s quite risky since it might be dead at some unexpected point.

This is one of most common problem, it is! Hence, from somewhere, the idea of ViewHolderpattern was created to improve this current situation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_list_item, null);
        ViewHolder holder = new ViewHolder();
        holder.txtName = (TextView)v.findViewById(R.id.txtName);
        holder.txtMail = (TextView)v.findViewById(R.id.txtMail);
        v.setTag(holder);
    }
    Contact entry = mList.get(position);
    if(entry != null) {
        ViewHolder holder = (ViewHolder)v.getTag();
        holder.txtName.setText(entry.getName());
        holder.txtMail.setText(entry.getMail());
    }
    return v;
}
static class ViewHolder {
    TextView txtName;
    TextView txtMail;
}

The ViewHolder object is static, once created, it will cache all things inside. Therefore, we can avoid calling ‘findViewById()‘ on resource everytime.

D – Note

– Something works doesn’t mean it’s the best. Even if it’s fine, it might contain some invisible problems.

– Use ViewHolder pattern whenever possible to cache data!

E – Final Words

– Just something I’ve learned and wanna share to everyone.

– Feel free to comment, suggest and request; I’m here to listen to everyone’s voice :)

– Free to share, free to take!

Cheers,

Pete Houston

From: http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/