2012-05-06 18 views
5

मैं इस बहुत उपयोगी साइट पर कुछ वापस रखना चाहता हूं, इसलिए यह वास्तव में एक प्रश्न नहीं है, बल्कि इस मुद्दे का मेरा समाधान है। मैं यह भी जोड़ूंगा कि इस समाधान को इस साइट और कई अन्य लोगों से फ़ॉर्म समर्थन मिला है, इसलिए यह कई अन्य डेवलपर्स के संयुक्त प्रयासों का प्रतिनिधित्व करता है। उनके लिए मैं आपको धन्यवाद देता हूं!एंड्रॉइड पेज कंट्रोल जैसे आईफोन पेज कंट्रोल

प्रश्न "आप क्षैतिज स्क्रॉल को फिर से कैसे बना सकते हैं आईफोन ऐप्स के पहलुओं और एंड्रॉइड पर्यावरण में संबंधित पेज नियंत्रण?"

यह उठ गया क्योंकि मैं नुस्खा में कदम प्रदर्शित करना चाहता था, एक ही स्क्रॉल व्यू में प्रत्येक चरण और आवश्यक सामग्री के लिए संबंधित विधि। मैं यह भी चाहता था कि पेज पर नियंत्रण उपयोगकर्ता को प्रदर्शित किया जाए जहां वे चरणों में थे और उन्हें किसी भी विशिष्ट चरण

मेरे ऐप का यह हिस्सा नुस्खा में चरणों को प्रदर्शित करता है। प्रत्येक चरण एक पृष्ठ पर प्रकट होता है और इसमें तीन घटक होते हैं। एक चरण पहचानकर्ता (यानी चरण 1, चरण 2), एक विधि और चरण के लिए आवश्यक सामग्री।

नुस्खा अनुभाग के नीचे हम एक पृष्ठ नियंत्रण प्रदर्शित करते हैं जो दिखाता है कि कौन सा पृष्ठ सक्रिय है और विशिष्ट पृष्ठों पर नेविगेट करने के लिए इसका उपयोग किया जा सकता है। आप देखेंगे कि पृष्ठ नियंत्रण में छवि बटन हैं और दो छवियां सरल मंडल हैं, एक चयनित पृष्ठ (पृष्ठ.png) के लिए एक और चयनित पृष्ठ के लिए एक (पृष्ठ_selected.png)

जब गतिविधि बनाई जाती है चयनित नुस्खा के लिए कदम डेटा से पुनर्प्राप्त किए जाते हैं और नुस्खा में प्रत्येक चरण के लिए दृश्य जोड़कर बनाए गए स्क्रोलर अनुभाग को पुनर्प्राप्त किया जाता है। जब आप स्क्रोलर स्वाइप दृश्य अगले या पिछले पृष्ठ पर लेता है और उसमें पेजर प्रदर्शन से संकेत मिलता है जो आप जिस पृष्ठ पर हैं अद्यतन किया जाता है

पहले 3 एक्सएमएल लेआउट (res/लेआउट)

recipe.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

<!--Scroller section--> 

    <HorizontalScrollView 
     android:id="@+id/scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="320dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="100dp" > 

      <LinearLayout 
       android:id="@+id/methodScrollView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" > 
      </LinearLayout> 

    </HorizontalScrollView> 

<!-- pager section --> 

    <LinearLayout 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="20dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="430dp" 
     android:gravity="center" 
     android:orientation="horizontal" > 

    </LinearLayout> 

</RelativeLayout> 

recipesscroll.xml (राय यह है कि प्रत्येक नुस्खा कदम के लिए स्क्रोलर अनुभाग में जोड़ दिया जाएगा। नोट स्क्रोलर अनुभाग recipeViewController.java में एक onTouchlistner है कि पेज स्क्रॉल को संभालने के लिए)

012,
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/recipeScroll" 
    android:layout_width="320dp" 
    android:layout_height="320dp" 
    android:gravity="center_vertical" > 

    <TextView 
    android:id="@+id/method" 
    style="@style/scrollMethod" 
    android:layout_width="wrap_content" 
    android:layout_height="200dp" 
    android:layout_alignParentTop="true" 
    android:text="Method" /> 

    <TextView 
    android:id="@+id/ingredients" 
    style="@style/scrollIngredients" 
    android:layout_width="wrap_content" 
    android:layout_height="120dp" 
    android:layout_alignParentTop="true" 
    android:text="Ingredients" /> 

    <TextView 
    android:id="@+id/methodStep" 
    style="@style/scrollStep" 
    android:layout_width="wrap_content" 
    android:layout_height="20dp" 
    android:layout_alignParentTop="true" 
    android:text="Step" /> 

</RelativeLayout> 

recipiespager.xml (दृश्य जो प्रत्येक नुस्खा चरण के लिए पेजर अनुभाग में जोड़ा जाएगा। ध्यान दें कि इनमें से प्रत्येक recipeViewController.java में एक onClick घटना है कि पेजर नियंत्रण में चयनित विशिष्ट पेज)

<?xml version="1.0" encoding="utf-8"?> 
<Button xmlns:android="http://schemas.android.com/apk/res/android" 
      style="@style/pageButton" 
      android:layout_marginLeft="10dp" 
      android:layout_width="16dp" 
      android:layout_height="16dp" 
      android:onClick="selectPage"> 

</Button> 

यह वह जगह है सब recipeViewController.java में

//my package name change this to yours 
package com.infactdata.spinAdinner; 

import java.util.ArrayList; 

//DataModel is the model for my data change this to yours or ignore 
because it is just away of holding the data that will populate the views 
import com.infactdata.plist.DataModel; 

import android.content.res.Resources; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.widget.HorizontalScrollView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class RecipeViewController extends RootViewController { 
    private DataModel currentData; 
    HorizontalScrollView h_scroll; 
    int numberOfPages = 0; 
    int thePage; 
    int otherPage; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //first of the xml files 
     setContentView(R.layout.recipe); 

     //reference to my global variables 
     GlobalClass global = (GlobalClass)getApplicationContext(); 

     //because I wanted a particular type face 
     Typeface face=Typeface.createFromAsset(getAssets(), "fonts/trebucit.ttf"); 

     //VERY IMPORTANT because we need to use this to add the content to the scroll 
     and pager sections 
     LayoutInflater inflater = getLayoutInflater(); 

     //current data held a dataModel 
     currentData = global.getCurrent(); 

     currentName.setText(currentData.getName()); 

     String imageFile = currentData.getImage(); 
     Resources r = getResources(); 

     int res = r.getIdentifier(imageFile, "drawable", "com.infactdata.spinAdinner"); 
     image.setImageResource(res); 

     //recources that change the pager indicators to different images 
     thePage = r.getIdentifier("page_selected", "drawable","com.infactdata.spinAdinner"); 
     otherPage = r.getIdentifier("page", "drawable", "com.infactdata.spinAdinner"); 

     //Get the method(ArrayList) out of the currentData(DataModel). This is the array of 
     data that will fill the added view with different content (ie. the specific 
     instructions for the recipe step. This could be your own data array. 

     ArrayList<String[]> method = new ArrayList<String[]>(); 
     method = currentData.getMethod(0); 
     numberOfPages = method.size(); 

     //now to build the views by adding the content and then adding the text for that 
     content that reflects the instructions for the step in the recipe 

     for(int i = 0; i < method.size(); i++){ 

      String[] methodStep = method.get(i); 

      //find the scroll view 
      LinearLayout scroll = (LinearLayout) findViewById(R.id.methodScrollView); 

      //find the recipe scroller. the second xml file 
      RelativeLayout step = (RelativeLayout)findViewById(R.id.recipeScroll); 

      //add the recipe step (step) to the scrollview (scroll) 
      step = (RelativeLayout)inflater.inflate(R.layout.recipescroll, scroll, false); 

      //add the instructions for this step in the recipe 
      TextView stage = (TextView)step.findViewById(R.id.methodStep); 
      stage.setText(methodStep[0].toString()); 
      stage.setTypeface(face); 

      TextView methodText = (TextView)step.findViewById(R.id.method); 
      methodText.setText(methodStep[1].toString()); 
      methodText.setTypeface(face); 

      TextView ingredients = (TextView)step.findViewById(R.id.ingredients); 
      ingredients.setText(methodStep[2].toString()); 
      ingredients.setTypeface(face); 

      //create method step and add to scroll 
      scroll.addView(step); 

      //pager setup is a duplicate of the above 
      //find the pager 
      LinearLayout pager = (LinearLayout) findViewById(R.id.pager); 

      //find the pager button. the third xml file 
      Button page = (Button)inflater.inflate(R.layout.recipespager, pager, false); 

      //give each button it own ID. This will be used to test which button should be highlighted and used to move to a specific page. This is because the ID is equal to the page number (0 based of course) 
      page.setId(i); 

      //because this is a fresh construction we will be on page 0 so highlight that button 
      if (i == 0){ 
       page.setBackgroundResource(thePage); 
      } 

      //create page control and add to pager 
      pager.addView(page); 
     } 

     //create the onTouch controls 

     h_scroll = (HorizontalScrollView) findViewById(R.id.scroll_view); 
     h_scroll.setOnTouchListener(scrolling); 

} 

private OnTouchListener scrolling = new OnTouchListener(){ 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == 
     MotionEvent.ACTION_CANCEL){ 
      int scrollX = h_scroll.getScrollX(); 
      int itemWidth = h_scroll.getMeasuredWidth(); 
      int activePage = ((scrollX + itemWidth/2)/itemWidth); 
      int scrollTo = activePage * itemWidth; 
      h_scroll.smoothScrollTo(scrollTo, 0); 


      //page control display the active page button 
      Log.v("MyDebug","Active page = "+activePage); 
      for(int i = 0; i < numberOfPages; i++){ 
       Button aPage = (Button) findViewById(i); 
       if(i == activePage){ 
        aPage.setBackgroundResource(thePage); 
       }else{ 
        aPage.setBackgroundResource(otherPage); 
       } 
      } 

      return true; 
     } else { 
      return false; 
     } 
    } 

}; 


//this is the onClick handler for the page buttons and moves the scroller to the page 
associated with the button. That is through the button ID, which matches the page 
number (0 based of course 

public void selectPage(View v) { 
    int newPage = v.getId(); 
    int itemWidth = h_scroll.getMeasuredWidth(); 
    int scrollTo = newPage * itemWidth; 
    h_scroll.smoothScrollTo(scrollTo, 0); 

    //page control display 
    Log.v("MyDebug","Active page = "+newPage); 
    for(int i = 0; i < numberOfPages; i++){ 
     Button aPage = (Button) findViewById(i); 
     if(i == newPage){ 
      aPage.setBackgroundResource(thePage); 
     }else{ 
      aPage.setBackgroundResource(otherPage); 
     } 
    } 
    } 

    public void finishActivity(View v){ 
     //perform back action 
     finish(); 
    } 

    public void nextActivity(View v){ 
     //move to next activity 
    } 
} 

एक साथ लाया करने के लिए स्क्रॉल होगा वैसे यह मेरा समाधान था। मुझे यकीन है कि वहां से मेरे पास बहुत चालाक प्रोग्रामर हैं इसलिए मुझे यकीन है कि कोई इसे सुधार सकता है। किसी भी तरह से धन्यवाद stackoverflow !!!!

+2

हैलो। इस सामग्री के लिए धन्यवाद। हालांकि आपको अपने समाधान के जवाब के रूप में अपने समाधान का प्रस्ताव देना चाहिए जैसा कि यहां सलाह दी गई है: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ –

+0

Amokrane, क्षमा करें मैं इसे सलाह के लिए कैसे बदल सकता हूँ? –

+0

ब्रायन, प्रश्न के रूप में प्रश्न/समस्या को छोड़ दें और समाधान को उत्तर के रूप में ले जाएं। –

उत्तर

0

मुझे लगता है कि ग्रीनड्रॉइड लाइब्रेरी आईफोन के यूआईपीएज कंट्रोल के समान कुछ हासिल करने में मदद करेगी।

बाजार में जीडीकेटल में अपने ऐप पर नज़र डालें। साथ ही आप अपनी इच्छित फाइलों को निकाल सकते हैं और पेज कंट्रोल बना सकते हैं। मैंने इसे अपने ऐप में इस्तेमाल किया है और यह ठीक काम करता है। इसे आसान बनाने के लिए थोड़ा अनुकूलन की आवश्यकता है।

https://github.com/cyrilmottier/GreenDroid

संबंधित मुद्दे