Thursday, February 21, 2019

Read poem app (2)

We want user can swipe to open new poem.
We use ViewParge to do this.
Xml file of new class now look like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageSwitcher
        android:id="@+id/ImageSwitcher01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/nen" >
    </ImageSwitcher>
  
    <LinearLayout
        android:id="@+id/l"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_marginTop="80dp"      
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>
No need textView because we will draw textView in code.
Add to top new class.
implements ViewPager.OnPageChangeListener
Declare more array to above Override.
static String[] ten2 = new String[8];
static String[] tacgia2 = new String[8];
static String[] ar = new String[8];
static String[] ar2 = new String[8];    
private ViewPager mViewPager;
static LinearLayout l;


Below the line keep screen on, change to.
l = (LinearLayout) findViewById(R.id.l);     
mViewPager = new ViewPager(this);       
mViewPager.setLayoutParams(new ListView.LayoutParams(
                ListView.LayoutParams.MATCH_PARENT,500));      
   mViewPager.setOnPageChangeListener(this);     
   mViewPager.setAdapter(new HeaderAdapter(this));     
   l.addView(mViewPager);
    Intent in = getIntent();
    Bundle bun = in.getExtras();        
    int vitri = bun.getInt("gi");
    String tenbai = bun.getString("gi2");


Now we read poems to String, add to array name ar.
for (int i = 0; i < mang.length; i++) { 
          InputStream inp = this.getResources().openRawResource((Integer) mang[i]);
          try
            {       
               byte[] buffer = new byte[inp.available()];
                while (inp.read(buffer) != -1);
                String jsontext = new String(buffer);                     
               ar[i]=jsontext;
            }
            catch (IOException ke)
            {
               //return null;
            }
            }       
Now we need to convert array, choosen poem will move to first, other continue after, last will back to first.
When user swipe on screen, it will show poems continuously.
for (int i = 0; i < 8; i++) {
 int so=vitri+i;
   if(so>7){
    so=so-8;
   }
  ten2[i]=ten[so];       
  tacgia2[i]=tacgia[so]; 
  ar2[i]=ar[so];
    }


Copy this lines to above last close bracket.
@Override
    public void onPageScrolled(int position,
            float positionOffset, int positionOffsetPixels) { }
    @Override
    public void onPageSelected(int position) { }
    @Override
    public void onPageScrollStateChanged(int state) {      
        boolean isScrolling =
                state != ViewPager.SCROLL_STATE_IDLE;        //ListView.requestDisallowInterceptTouchEvent(isScrolling);
    }
     private static class HeaderAdapter extends PagerAdapter {
        private Context mContext;
        public HeaderAdapter(Context context) {
          mContext = context;
        }       
    @Override
    public int getCount() {
        return 8;
    }    
     @Override
    public Object instantiateItem(ViewGroup container,int position) {            
    LinearLayout l = new LinearLayout(mContext);
    l.setOrientation(LinearLayout.VERTICAL);     
    TextView t = new TextView(mContext);
        TextView t2 = new TextView(mContext);
        TextView t3 = new TextView(mContext);      
        t.setGravity(Gravity.CENTER);
        t2.setGravity(Gravity.CENTER);
        t3.setGravity(Gravity.CENTER);
       
 t.setTextColor(Color.parseColor("#800000"));
 t.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
 t.setHeight(48);

 t2.setTextColor(Color.BLACK);
 t2.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "Roboto-Italic.ttf"));
 t3.setTextColor(Color.BLUE);
 t.setTextSize(20);
 t2.setTextSize(18);
 t3.setTextSize(20);

 t.setText(ten2[position]);
 t2.setText(ar2[position]);
 t3.setText(tacgia2[position]);

LayoutParams pa = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);    

 l.addView(t, pa);
 l.addView(t2, pa);
 l.addView(t3, pa);
   container.addView(l);    
    return l;
    }
   @Override
    public void destroyItem(ViewGroup container,int position, Object object) {
        View page = (View) object;
        container.removeView(page);
    }
    @Override
    public boolean isViewFromObject(View view,Object object) {
        return (view == object);
    }
 }  
This code.
@Override
    public int getCount() {
        return 8;
    }    
Number 8 is array size, change this nụmber if you have different poems quantity.
Run and swipe to new poem.



No comments:

Post a Comment