如何使用*突出显示功能*突出显示ListView中的项目?

时间:2016-02-22 02:42:49

标签: react-native

如果有人可以举例说明如何使用highlight function,我们将不胜感激。

在文档中,有:renderRow函数,(rowData,sectionID,rowID,highlightRow)=>渲染 "通过调用highlightRow函数突出显示行时,可以通知ListView。突出显示行时,将隐藏上方和下方的分隔符。可以通过调用highlightRow(null)来重置行的突出显示状态。"

我一遍又一遍地读它,然后我读了ListView的源文件,我仍然知道如何使用高亮功能。

是否提供了highlightRow功能供开发人员在需要时调用它?开发人员是否应该保留highlightRow的引用(如果需要的话)?调用highlightRow函数时应该提供什么参数?

3 个答案:

答案 0 :(得分:0)

@tennist,所有其他答案都是android代码。我希望您正在寻找一种特定于本地的反应解决方案。我也很想知道highlightRow功能是如何工作的。如果你搞清楚了,请告诉我。我怀疑它类似于添加到数据源的public static HttpClient wrapClient(HttpClient base) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", ssf, 443)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Exception ex) { return null; } 函数,但我玩了一秒钟并没有任何运气。但是,我确实想出了一个解决方案。这是我修改_renderRow函数的方法......

{rowHasChanged: (r1, r2) => r1 !== r2}

秘诀就在这里......

_renderRow: function(rowData: string, sectionID: number, rowID: number, highlightRow: func) {

var rowHash = Math.abs(hashCode(rowData));
var highlighted = {};
if(rowID == 5){
    highlighted = {color: 'red'};  
}

return (
  <TouchableHighlight onPress={() => console.log('[dbug] pressed', rowID)}>
    <View>
      <View style={styles.row}>
        <Text style={[styles.text,highlighted]}>
          {rowData + ' - ' + LOREM_IPSUM.substr(0, rowHash % 301 + 10)}
        </Text>
      </View>
    </View>
  </TouchableHighlight>
);
},

在这里,我已经任意决定应该突出显示rowID 5,但是你可以在那里实现对你的app有意义的任何逻辑。

答案 1 :(得分:-2)

以下是突出显示所选项目列表视图的代码。

ListView lstTemplate;
ArrayAdapter<String> listAdapter;
ArrayList<String> templateNames = new ArrayList<String>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    lstTemplate = (ListView) findViewById(R.id.lstTamplates);

    // Set the ArrayAdapter as the ListView's adapter.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, templateNames);
    lstTemplate.setAdapter( listAdapter );
    listAdapter.notifyDataSetChanged();

    lstTemplate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            if(i == 0) return; // if empty is chosen do nothing

            for (int j = 0; j < adapterView.getChildCount(); j++)
               adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

           // change the background color of the selected element
           view.setBackgroundColor(Color.LTGRAY);
           selectedItemIndex = i;
       }
   });
}

答案 2 :(得分:-2)

我记得也遇到过这方面的挑战。我不确定这是最好的方法..但是我在一年前写的一些代码中处理我的Activity类中的列表项是如何处理的:

public class MyListActivity extends Activity {

    ListView myListView;
    MyListAdapter myListAdapter;
    View currentlySelectedListItemView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list);

        myListAdapter = new MyListAdapter(this);

        myListView = (ListView) findViewById(R.id.song_list);

        myListView.setSelection(0);

        myListView.setAdapter(myistAdapter);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // un-highlight the last item that was selected
                if (currentlySelectedListItemView != null) {
                    currentlySelectedListItemView.setBackgroundColor(
                            Color.argb(0, 0, 0, 0)
                    );

                }

                // highlight the item that was selected
                view.setBackgroundColor(
                        getResources().getColor(R.color.blue));

                view.getBackground().setAlpha(90);

                currentlySelectedListItemView = view;
            }
        });

    }
相关问题