2015-05-06 18 views
8

Come implementare l'hashtag all'interno di TextView? Quello che voglio fare è implementare l'hashtag collegabile all'interno di textview. Quindi l'utente può fare clic su di esso (hashtag) e passare a un altro frammento. Questo è il mio layout e il mio frammento.Android: hashtag in TextView

layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainFragment" > 

    <TextView 
      android:id="@+id/txtHashtag" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Iena I #love you !!!"/> 
</RelativeLayout> 

Frammento

package com.xxxx; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.xxxxx.R; 

public class MainFragment extends Fragment{ 

    private TextView txtHashtag; 
    public MainFragment() { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View v = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main_fragment, 
       null); 

     txtHashtag = (TextView) v.findViewById(R.id.txtHashtag); 

     return v; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
    } 
} 

consiglio prega. Grazie.

+0

Guarda questo esempio, mi ha aiutato molto: [inserisci link de scription here] (http://stackoverflow.com/questions/25363310/clickable-words-inside-of-textview-android) – sunlover3

risposta

0

Qualunque sia la vista che si desidera che risponda con i clic, è necessario allegare un onClickListener proprio come hai implementato sul pulsante (supponendo che tu abbia capito i meccanismi dei clic sui pulsanti).

pertanto, è possibile avere in questo modo:

txtHashtag.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // This is where you call the method to change fragment 
     } 
    }); 

Quanto al metodo per andare a detto frammento, assicurarsi di avere quel frammento configurato per ricevere parametri durante istanziazione come tale:

public class HashtagViewFragment extends Fragment { 
private static final String TAG = "HashtagViewFragment"; 
public static final String PARAM_HASHTAG = "hashtag_string"; 

private TextView txtPassedString; 

private String receivedString; 

public static HashtagViewFragment newInstance(String myHashtag) { 
    HashtagViewFragment fragment = new HashtagViewFragment(); 
    Bundle args = new Bundle(); 
    args.putString(PARAM_HASHTAG, myHashtag); 
    fragment.setArguments(args); 
    return fragment; 
} 

public HashtagViewFragment() { 
    // Required empty public constructor 
} 

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

    if (getArguments() != null) { 
     receivedString = getArguments().getString(PARAM_HASHTAG); 
    } else { 
     receivedString = ""; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.HashtagViewFragment, container, false); 

    txtPassedString = (TextView) view.findById(R.id.tv_hashtag); 
    txtPassedString.setText(receivedString); 

    return view; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
} 
} 

quindi, l'adattatore ViewPager deve essere configurato come tale da poter istanziare i frammenti su richiesta in questo modo:

public class HashtagPagerAdapter extends FragmentStatePagerAdapter { 
private Context mContext; 
private ArrayList<String> hashtags = new ArrayList<>(); 

public HashtagPagerAdapter(FragmentManager fm, Context ctx) { 
    super(fm); 
    this.mContext = ctx; 
} 

@Override 
public Fragment getItem(int position) { 
    switch (position) { 
     case 0: 
      return new MainFragment(); 
     default: 
      return HashtagViewFragment.newInstance(hashtags.get(position - 1)); 
    } 
} 

private void setHashtags(ArrayList<String> latestHashtags) { 
    this.hashtags = latestHashtags; 
    notifyDataSetChanged(); 
} 

private ArrayList<String> getHashtags() { 
    return this.hashtags; 
} 

@Override 
public int getCount() { 
    return hashtags.size() + 1; 
} 

} 

A condizione che il ViewPager è statico pubblico nell'oggetto FragmentActivity, è possibile accedervi dal proprio MainFragment in questo modo:

MyFragmentActivity.myViewPager 

Con questo in mente, si può ora creare il metodo per aggiungere e frammento di swap come segue:

private void swapFragment() { 
    ArrayList<String> currentHashtags = ((HashtagPagerAdapter)MyFragmentActivity.myViewPager.getAdapter()).getHashtags(); 
    currentHashtags.add(txtHashtag.getText().toString()); 
    ((HashtagPagerAdapter)MyFragmentActivity.myViewPager.getAdapter()).setHashtags(currentHashtags); 
    MyFragmentActivity.myViewPager.setCurrentItem(currentHashtags.size(), true); 
} 
2

Utilizzare questo, cliccabile Span in TextView

// setting span 

    SpannableString tagSpan = new SpannableString("#clickMe"); 
    ClickableSpan clickSpan = new ClickableSpan() { 
     @Override 
     public void onClick(View textView) { 
      //code to swtich to new fragment 
     } 
     @Override 
     public void updateDrawState(TextPaint paint) { 
      super.updateDrawState(paint); 
      paint.setUnderlineText(true); // set underline if you want to underline 
      paint.setColor(Color.BLUE); // set the color to blue 
     } 
    }; 
    tagSpan.setSpan(clickSpan, startPosition, endPosition, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

    TextView txtHashtag = (TextView) findViewById(R.id.txtHashtag); 
    txtHashtag.setText(tagSpan); 
    txtHashtag.setMovementMethod(LinkMovementMethod.getInstance()); 

È possibile implementare più di una arco in un textView, così semplicemente scrivere una funzione per farlo e lo chiamano per ogni #HashTag

11

È possibile utilizzare questa semplice libreria HashTagHelper

Ha utilizzo molto semplice:

mHashTagText = (TextView) findViewById(R.id.text); 
mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), 
    new HashTagHelper.OnHashTagClickListener() { 
     @Override 
     public void onHashTagClicked(String hashTag) { 

     } 
}); 

// pass a TextView or any descendant of it (incliding EditText) here. 
// Hash tags that are in the text will be hightlighed with a color passed to HasTagHelper 

mTextHashTagHelper.handle(mHashTagText); 
+0

Grazie per il tuo contributo, purtroppo il tuo codice sopra non ha funzionato per me. Ma quando scarico il progetto di esempio da https://github.com/danylovolokh/HashTagHelper e copio i codici necessari da utilizzare nel mio progetto, allora ha funzionato correttamente. – oguzhan

1

Prova di seguito codice per abbinare TextView testo che inizia con #:

... 
txtHashtag = (TextView) v.findViewById(R.id.txtHashtag); 
//Pattern to find if there's a hash tag in the message 

      Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");  

      String url = "https://www.google.co.in/"; 
      //Attach Linkify to TextView 
      Linkify.addLinks(txtHashtag, tagMatcher, url); 
...